diff --git a/examples/lambda/README.md b/examples/lambda/README.md new file mode 100644 index 00000000000..d93dde30d75 --- /dev/null +++ b/examples/lambda/README.md @@ -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. diff --git a/examples/lambda/hello_lambda.py b/examples/lambda/hello_lambda.py new file mode 100644 index 00000000000..3c3b6bf9a23 --- /dev/null +++ b/examples/lambda/hello_lambda.py @@ -0,0 +1,4 @@ +import os + +def lambda_handler(event, context): + return "{} from Lambda!".format(os.environ['greeting']) diff --git a/examples/lambda/main.tf b/examples/lambda/main.tf new file mode 100644 index 00000000000..9569641bea7 --- /dev/null +++ b/examples/lambda/main.tf @@ -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" + } + } +} diff --git a/examples/lambda/outputs.tf b/examples/lambda/outputs.tf new file mode 100644 index 00000000000..d3a6aba024b --- /dev/null +++ b/examples/lambda/outputs.tf @@ -0,0 +1,3 @@ +output "lambda" { + value = "${aws_lambda_function.lambda.qualified_arn}" +} diff --git a/examples/lambda/variables.tf b/examples/lambda/variables.tf new file mode 100644 index 00000000000..78938923066 --- /dev/null +++ b/examples/lambda/variables.tf @@ -0,0 +1,4 @@ +variable "aws_region" { + description = "The AWS region to create things in." + default = "us-east-1" +}