-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from tungbq/issue-161
Issue 161: Create lamda function
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
tutorial/serverless-basic-application/assets/lamda_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# import the JSON utility package since we will be working with a JSON object | ||
import json | ||
# define the handler function that the Lambda service will use as an entry point | ||
def lambda_handler(event, context): | ||
# extract values from the event object we got from the Lambda service | ||
name = event['firstName'] +' '+ event['lastName'] | ||
# return a properly formatted JSON object | ||
return { | ||
'statusCode': 200, | ||
'body': json.dumps('Hello from Lambda, ' + name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Saving sourcecode to s3 | ||
data "archive_file" "lambda_hello_world" { | ||
type = "zip" | ||
|
||
source_dir = "${path.module}/assets" | ||
output_path = "${path.module}/assets/lamda_function.zip" | ||
} | ||
|
||
resource "aws_s3_bucket" "tungbq_lamda_source" { | ||
bucket = "tungbq-lamda-source" | ||
|
||
tags = { | ||
Name = "S3 bucket to store lamda source code" | ||
Environment = "Dev" | ||
} | ||
} | ||
|
||
|
||
resource "aws_s3_object" "lambda_hello_world" { | ||
bucket = aws_s3_bucket.tungbq_lamda_source.id | ||
|
||
key = "lamda_function.zip" | ||
source = data.archive_file.lambda_hello_world.output_path | ||
|
||
etag = filemd5(data.archive_file.lambda_hello_world.output_path) | ||
} | ||
|
||
resource "aws_lambda_function" "hello_world" { | ||
function_name = "HelloWorld" | ||
|
||
s3_bucket = aws_s3_bucket.tungbq_lamda_source.id | ||
s3_key = aws_s3_object.lambda_hello_world.key | ||
|
||
runtime = "python3.8" | ||
handler = "lamda_function.lambda_handler" | ||
|
||
source_code_hash = data.archive_file.lambda_hello_world.output_base64sha256 | ||
|
||
role = aws_iam_role.lambda_exec.arn | ||
} | ||
|
||
resource "aws_cloudwatch_log_group" "hello_world" { | ||
name = "/aws/lambda/${aws_lambda_function.hello_world.function_name}" | ||
|
||
retention_in_days = 30 | ||
} | ||
|
||
resource "aws_iam_role" "lambda_exec" { | ||
name = "serverless_lambda" | ||
|
||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [{ | ||
Action = "sts:AssumeRole" | ||
Effect = "Allow" | ||
Sid = "" | ||
Principal = { | ||
Service = "lambda.amazonaws.com" | ||
} | ||
} | ||
] | ||
}) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "lambda_policy" { | ||
role = aws_iam_role.lambda_exec.name | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
output "function_name" { | ||
description = "Name of the Lambda function." | ||
|
||
value = aws_lambda_function.hello_world.function_name | ||
} |