-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Add lambda example #3168
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.zip | ||
*.dll | ||
*.exe | ||
.DS_Store | ||
|
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to enhance this message with: Just to expose my point of view: another way to deploy a given lambda is by building the ZIP in your CI, put it on S3, and use a s3 object data source to get the file. Thoughts? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's an improvement. I'll add it. |
||
|
||
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import os | ||
|
||
def lambda_handler(event, context): | ||
# This will show up in CloudWatch | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should either update this message to In both cases, we should rewrite it to What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will remove logging because it's not that clear cut due to the AWS console. If you run the lambda you'll see the following under Log output:
If we were to print There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @oscr My question was meant for the comment line only, not the log line that would appear in CloudWatch Logs 😄, sorry for the confusion! |
||
print("Value of 'foo': " + os.environ['foo']) | ||
return 'Hello from Lambda!' |
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 = { | ||
foo = "bar" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "lambda" { | ||
value = "${aws_lambda_function.lambda.qualified_arn}" | ||
} |
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" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually have a few examples where we need those zips, as in: https://github.com/terraform-providers/terraform-provider-aws/tree/master/examples/cognito-user-pool so we should remov this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will fix that mistake.