Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lambda example #3168

Merged
merged 2 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/lambda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Lambda Example

This examples shows how to deploy an AWS Lambda function using Terraform only.

To run, configure your AWS provider as described in https://www.terraform.io/docs/providers/aws/index.html

Running the example

run `terraform apply` to see it work.
4 changes: 4 additions & 0 deletions examples/lambda/hello_lambda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os

def lambda_handler(event, context):
return "{} from Lambda!".format(os.environ['greeting'])
48 changes: 48 additions & 0 deletions examples/lambda/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Specify the provider and access details
provider "aws" {
region = "${var.aws_region}"
}

provider "archive" {}

data "archive_file" "zip" {
type = "zip"
source_file = "hello_lambda.py"
output_path = "hello_lambda.zip"
}

data "aws_iam_policy_document" "policy" {
statement {
sid = ""
effect = "Allow"

principals {
identifiers = ["lambda.amazonaws.com"]
type = "Service"
}

actions = ["sts:AssumeRole"]
}
}

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = "${data.aws_iam_policy_document.policy.json}"
}

resource "aws_lambda_function" "lambda" {
function_name = "hello_lambda"

filename = "${data.archive_file.zip.output_path}"
source_code_hash = "${data.archive_file.zip.output_sha}"

role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "hello_lambda.lambda_handler"
runtime = "python3.6"

environment {
variables = {
greeting = "Hello"
}
}
}
3 changes: 3 additions & 0 deletions examples/lambda/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "lambda" {
value = "${aws_lambda_function.lambda.qualified_arn}"
}
4 changes: 4 additions & 0 deletions examples/lambda/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "aws_region" {
description = "The AWS region to create things in."
default = "us-east-1"
}