From dcb61f1a89ab510da37cb99654ce8a3e690a02bb Mon Sep 17 00:00:00 2001 From: Robin Norwood Date: Thu, 15 Jul 2021 11:44:46 -0500 Subject: [PATCH 1/3] Final configuration after following the tutorial --- hello-world/hello.js | 4 ++ main.tf | 118 +++++++++++++++++++++++++++++++++++++++++++ outputs.tf | 12 +++++ 3 files changed, 134 insertions(+) diff --git a/hello-world/hello.js b/hello-world/hello.js index 0ee2558..ae8798f 100644 --- a/hello-world/hello.js +++ b/hello-world/hello.js @@ -4,6 +4,10 @@ module.exports.handler = async (event) => { console.log('Event: ', event) let responseMessage = 'Hello, World!'; + if (event.queryStringParameters && event.queryStringParameters['Name']) { + responseMessage = 'Hello, ' + event.queryStringParameters['Name'] + '!'; + } + return { statusCode: 200, headers: { diff --git a/main.tf b/main.tf index a21afc9..bd0f5fc 100644 --- a/main.tf +++ b/main.tf @@ -32,3 +32,121 @@ resource "aws_s3_bucket" "lambda_bucket" { acl = "private" force_destroy = true } + +data "archive_file" "lambda_hello_world" { + type = "zip" + + source_dir = "${path.module}/hello-world" + output_path = "${path.module}/hello-world.zip" +} + +resource "aws_s3_bucket_object" "lambda_hello_world" { + bucket = aws_s3_bucket.lambda_bucket.id + + key = "hello-world.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.lambda_bucket.id + s3_key = aws_s3_bucket_object.lambda_hello_world.key + + runtime = "nodejs12.x" + handler = "hello.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" +} + +resource "aws_apigatewayv2_api" "lambda" { + name = "serverless_lambda_gw" + protocol_type = "HTTP" +} + +resource "aws_apigatewayv2_stage" "lambda" { + api_id = aws_apigatewayv2_api.lambda.id + + name = "serverless_lambda_stage" + auto_deploy = true + + access_log_settings { + destination_arn = aws_cloudwatch_log_group.api_gw.arn + + format = jsonencode({ + requestId = "$context.requestId" + sourceIp = "$context.identity.sourceIp" + requestTime = "$context.requestTime" + protocol = "$context.protocol" + httpMethod = "$context.httpMethod" + resourcePath = "$context.resourcePath" + routeKey = "$context.routeKey" + status = "$context.status" + responseLength = "$context.responseLength" + integrationErrorMessage = "$context.integrationErrorMessage" + } + ) + } +} + +resource "aws_apigatewayv2_integration" "hello_world" { + api_id = aws_apigatewayv2_api.lambda.id + + integration_uri = aws_lambda_function.hello_world.invoke_arn + integration_type = "AWS_PROXY" + integration_method = "POST" +} + +resource "aws_apigatewayv2_route" "hello_world" { + api_id = aws_apigatewayv2_api.lambda.id + + route_key = "GET /hello" + target = "integrations/${aws_apigatewayv2_integration.hello_world.id}" +} + +resource "aws_cloudwatch_log_group" "api_gw" { + name = "/aws/api_gw/${aws_apigatewayv2_api.lambda.name}" + + retention_in_days = 30 +} + +resource "aws_lambda_permission" "api_gw" { + statement_id = "AllowExecutionFromAPIGateway" + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.hello_world.function_name + principal = "apigateway.amazonaws.com" + + source_arn = "${aws_apigatewayv2_api.lambda.execution_arn}/*/*" +} diff --git a/outputs.tf b/outputs.tf index cdc3da2..67bc0fb 100644 --- a/outputs.tf +++ b/outputs.tf @@ -5,3 +5,15 @@ output "lambda_bucket_name" { value = aws_s3_bucket.lambda_bucket.id } + +output "function_name" { + description = "Name of the Lambda function." + + value = aws_lambda_function.hello_world.function_name +} + +output "base_url" { + description = "Base URL for API Gateway stage." + + value = aws_apigatewayv2_stage.lambda.invoke_url +} From d1ca758bdea24eaec4f6e44f0ff2bb7bbb6e8c39 Mon Sep 17 00:00:00 2001 From: Robin Norwood Date: Thu, 15 Jul 2021 13:27:46 -0500 Subject: [PATCH 2/3] Add semicolon --- hello-world/hello.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hello-world/hello.js b/hello-world/hello.js index ae8798f..f9fce96 100644 --- a/hello-world/hello.js +++ b/hello-world/hello.js @@ -1,7 +1,7 @@ // Lambda function code module.exports.handler = async (event) => { -console.log('Event: ', event) + console.log('Event: ', event); let responseMessage = 'Hello, World!'; if (event.queryStringParameters && event.queryStringParameters['Name']) { From 6928e6c0437e585b209a5ee14c6f855f4b9c6204 Mon Sep 17 00:00:00 2001 From: Robin Norwood Date: Wed, 2 Feb 2022 09:52:55 -0600 Subject: [PATCH 3/3] Switch to AWS Provider 4 --- main.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index bd0f5fc..a7a8df8 100644 --- a/main.tf +++ b/main.tf @@ -2,7 +2,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = "~> 3.48.0" + version = "~> 4.0.0" } random = { source = "hashicorp/random" @@ -40,7 +40,7 @@ data "archive_file" "lambda_hello_world" { output_path = "${path.module}/hello-world.zip" } -resource "aws_s3_bucket_object" "lambda_hello_world" { +resource "aws_s3_object" "lambda_hello_world" { bucket = aws_s3_bucket.lambda_bucket.id key = "hello-world.zip" @@ -53,7 +53,7 @@ resource "aws_lambda_function" "hello_world" { function_name = "HelloWorld" s3_bucket = aws_s3_bucket.lambda_bucket.id - s3_key = aws_s3_bucket_object.lambda_hello_world.key + s3_key = aws_s3_object.lambda_hello_world.key runtime = "nodejs12.x" handler = "hello.handler"