From 23cf9d31cf87f6f6db196afd2dcab1cf4dfc6b1b Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 12:07:37 +0700 Subject: [PATCH 1/9] i162 - init API gateway --- .../api-gateway.tf | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tutorial/serverless-basic-application/api-gateway.tf diff --git a/tutorial/serverless-basic-application/api-gateway.tf b/tutorial/serverless-basic-application/api-gateway.tf new file mode 100644 index 0000000..59a6bfa --- /dev/null +++ b/tutorial/serverless-basic-application/api-gateway.tf @@ -0,0 +1,59 @@ +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}/*/*" +} From 686f06fe5c4e55ac53ae0fab4ec054414ae8f5a2 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 12:18:28 +0700 Subject: [PATCH 2/9] improve output --- tutorial/serverless-basic-application/api-gateway.tf | 2 +- tutorial/serverless-basic-application/outputs.tf | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tutorial/serverless-basic-application/api-gateway.tf b/tutorial/serverless-basic-application/api-gateway.tf index 59a6bfa..2fd256a 100644 --- a/tutorial/serverless-basic-application/api-gateway.tf +++ b/tutorial/serverless-basic-application/api-gateway.tf @@ -39,7 +39,7 @@ resource "aws_apigatewayv2_integration" "hello_world" { resource "aws_apigatewayv2_route" "hello_world" { api_id = aws_apigatewayv2_api.lambda.id - route_key = "GET /hello" + route_key = "POST /" target = "integrations/${aws_apigatewayv2_integration.hello_world.id}" } diff --git a/tutorial/serverless-basic-application/outputs.tf b/tutorial/serverless-basic-application/outputs.tf index 3fb7f0a..997c925 100644 --- a/tutorial/serverless-basic-application/outputs.tf +++ b/tutorial/serverless-basic-application/outputs.tf @@ -3,3 +3,9 @@ output "function_name" { 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 7c1df76c4e8dc476c32c6c75a47bf08b5ed08603 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 12:44:26 +0700 Subject: [PATCH 3/9] i162 - fix api deployment --- .../api-gateway.tf | 50 ++++++++++++------- .../serverless-basic-application/outputs.tf | 2 +- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/tutorial/serverless-basic-application/api-gateway.tf b/tutorial/serverless-basic-application/api-gateway.tf index 2fd256a..139b321 100644 --- a/tutorial/serverless-basic-application/api-gateway.tf +++ b/tutorial/serverless-basic-application/api-gateway.tf @@ -1,13 +1,15 @@ -resource "aws_apigatewayv2_api" "lambda" { +# Flow: New API -> Resource -> Method -> Interation -> Deployment + + +resource "aws_api_gateway_rest_api" "lambda" { name = "serverless_lambda_gw" - protocol_type = "HTTP" } -resource "aws_apigatewayv2_stage" "lambda" { - api_id = aws_apigatewayv2_api.lambda.id +resource "aws_api_gateway_stage" "lambda" { + rest_api_id = aws_api_gateway_rest_api.lambda.id + deployment_id = aws_api_gateway_deployment.example.id - name = "serverless_lambda_stage" - auto_deploy = true + stage_name = "serverless_lambda_stage" access_log_settings { destination_arn = aws_cloudwatch_log_group.api_gw.arn @@ -28,23 +30,35 @@ resource "aws_apigatewayv2_stage" "lambda" { } } -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_api_gateway_resource" "resource" { + path_part = "resource" + parent_id = aws_api_gateway_rest_api.lambda.root_resource_id + rest_api_id = aws_api_gateway_rest_api.lambda.id } -resource "aws_apigatewayv2_route" "hello_world" { - api_id = aws_apigatewayv2_api.lambda.id - route_key = "POST /" - target = "integrations/${aws_apigatewayv2_integration.hello_world.id}" +resource "aws_api_gateway_deployment" "example" { + rest_api_id = aws_api_gateway_rest_api.lambda.id + + triggers = { + redeployment = sha1(jsonencode(aws_api_gateway_rest_api.lambda.body)) + } + + lifecycle { + create_before_destroy = true + } +} + +resource "aws_api_gateway_integration" "integration" { + rest_api_id = aws_api_gateway_rest_api.lambda.id + resource_id = aws_api_gateway_resource.resource.id + http_method = "POST" + type = "AWS_PROXY" + uri = aws_lambda_function.hello_world.invoke_arn } resource "aws_cloudwatch_log_group" "api_gw" { - name = "/aws/api_gw/${aws_apigatewayv2_api.lambda.name}" + name = "/aws/api_gw/${aws_api_gateway_rest_api.lambda.name}" retention_in_days = 30 } @@ -55,5 +69,5 @@ resource "aws_lambda_permission" "api_gw" { function_name = aws_lambda_function.hello_world.function_name principal = "apigateway.amazonaws.com" - source_arn = "${aws_apigatewayv2_api.lambda.execution_arn}/*/*" + source_arn = "${aws_api_gateway_rest_api.lambda.execution_arn}/*/*" } diff --git a/tutorial/serverless-basic-application/outputs.tf b/tutorial/serverless-basic-application/outputs.tf index 997c925..f39503a 100644 --- a/tutorial/serverless-basic-application/outputs.tf +++ b/tutorial/serverless-basic-application/outputs.tf @@ -7,5 +7,5 @@ output "function_name" { output "base_url" { description = "Base URL for API Gateway stage." - value = aws_apigatewayv2_stage.lambda.invoke_url + value = aws_api_gateway_stage.lambda.invoke_url } From 44626b2489144568279ad882a94bc1d01583bee8 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 12:48:34 +0700 Subject: [PATCH 4/9] Fix api gateway deployment --- tutorial/serverless-basic-application/api-gateway.tf | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tutorial/serverless-basic-application/api-gateway.tf b/tutorial/serverless-basic-application/api-gateway.tf index 139b321..e9c0626 100644 --- a/tutorial/serverless-basic-application/api-gateway.tf +++ b/tutorial/serverless-basic-application/api-gateway.tf @@ -41,7 +41,12 @@ resource "aws_api_gateway_deployment" "example" { rest_api_id = aws_api_gateway_rest_api.lambda.id triggers = { - redeployment = sha1(jsonencode(aws_api_gateway_rest_api.lambda.body)) + # redeployment = sha1(jsonencode(aws_api_gateway_rest_api.lambda.body)) + redeployment = sha1(jsonencode([ + aws_api_gateway_resource.resource.id, + # aws_api_gateway_method.example.id, + aws_api_gateway_integration.integration.id, + ])) } lifecycle { @@ -52,7 +57,7 @@ resource "aws_api_gateway_deployment" "example" { resource "aws_api_gateway_integration" "integration" { rest_api_id = aws_api_gateway_rest_api.lambda.id resource_id = aws_api_gateway_resource.resource.id - http_method = "POST" + http_method = "POST" type = "AWS_PROXY" uri = aws_lambda_function.hello_world.invoke_arn } From a881596c49a162b1fd4cad564aa5e477aac27e15 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 12:53:14 +0700 Subject: [PATCH 5/9] Fix api gateway deployment.v1 --- .../api-gateway.tf | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tutorial/serverless-basic-application/api-gateway.tf b/tutorial/serverless-basic-application/api-gateway.tf index e9c0626..745c9e1 100644 --- a/tutorial/serverless-basic-application/api-gateway.tf +++ b/tutorial/serverless-basic-application/api-gateway.tf @@ -30,13 +30,32 @@ resource "aws_api_gateway_stage" "lambda" { } } +# Resource resource "aws_api_gateway_resource" "resource" { path_part = "resource" parent_id = aws_api_gateway_rest_api.lambda.root_resource_id rest_api_id = aws_api_gateway_rest_api.lambda.id } +# Method +resource "aws_api_gateway_method" "method" { + rest_api_id = aws_api_gateway_rest_api.lambda.id + resource_id = aws_api_gateway_resource.resource.id + http_method = "POST" + authorization = "NONE" +} + +# Integration +resource "aws_api_gateway_integration" "integration" { + rest_api_id = aws_api_gateway_rest_api.lambda.id + resource_id = aws_api_gateway_resource.resource.id + http_method = aws_api_gateway_method.method.http_method + integration_http_method = "POST" + type = "AWS_PROXY" + uri = aws_lambda_function.hello_world.invoke_arn +} +# Deployment resource "aws_api_gateway_deployment" "example" { rest_api_id = aws_api_gateway_rest_api.lambda.id @@ -54,14 +73,7 @@ resource "aws_api_gateway_deployment" "example" { } } -resource "aws_api_gateway_integration" "integration" { - rest_api_id = aws_api_gateway_rest_api.lambda.id - resource_id = aws_api_gateway_resource.resource.id - http_method = "POST" - type = "AWS_PROXY" - uri = aws_lambda_function.hello_world.invoke_arn -} - +# Logging resource "aws_cloudwatch_log_group" "api_gw" { name = "/aws/api_gw/${aws_api_gateway_rest_api.lambda.name}" From b72a26489c150621697995c03d5c04600c6c6b50 Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 12:58:03 +0700 Subject: [PATCH 6/9] apigateway: temporarity disable logging --- .../api-gateway.tf | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/tutorial/serverless-basic-application/api-gateway.tf b/tutorial/serverless-basic-application/api-gateway.tf index 745c9e1..fffcf3f 100644 --- a/tutorial/serverless-basic-application/api-gateway.tf +++ b/tutorial/serverless-basic-application/api-gateway.tf @@ -11,23 +11,24 @@ resource "aws_api_gateway_stage" "lambda" { stage_name = "serverless_lambda_stage" - 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" - } - ) - } + # Temporarity disable logging. TODO: work on it later + # 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 @@ -74,11 +75,12 @@ resource "aws_api_gateway_deployment" "example" { } # Logging -resource "aws_cloudwatch_log_group" "api_gw" { - name = "/aws/api_gw/${aws_api_gateway_rest_api.lambda.name}" +# Temporarity disable logging. TODO: work on it later +# resource "aws_cloudwatch_log_group" "api_gw" { +# name = "/aws/api_gw/${aws_api_gateway_rest_api.lambda.name}" - retention_in_days = 30 -} +# retention_in_days = 30 +# } resource "aws_lambda_permission" "api_gw" { statement_id = "AllowExecutionFromAPIGateway" From cf2af0d86d88d8017a34803a50a2eacdaaec915c Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 13:03:22 +0700 Subject: [PATCH 7/9] i162: bump first working ver --- tutorial/serverless-basic-application/api-gateway.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tutorial/serverless-basic-application/api-gateway.tf b/tutorial/serverless-basic-application/api-gateway.tf index fffcf3f..ceedaf7 100644 --- a/tutorial/serverless-basic-application/api-gateway.tf +++ b/tutorial/serverless-basic-application/api-gateway.tf @@ -1,10 +1,10 @@ # Flow: New API -> Resource -> Method -> Interation -> Deployment - resource "aws_api_gateway_rest_api" "lambda" { name = "serverless_lambda_gw" } +# Stage resource "aws_api_gateway_stage" "lambda" { rest_api_id = aws_api_gateway_rest_api.lambda.id deployment_id = aws_api_gateway_deployment.example.id @@ -64,7 +64,7 @@ resource "aws_api_gateway_deployment" "example" { # redeployment = sha1(jsonencode(aws_api_gateway_rest_api.lambda.body)) redeployment = sha1(jsonencode([ aws_api_gateway_resource.resource.id, - # aws_api_gateway_method.example.id, + aws_api_gateway_method.method.id, aws_api_gateway_integration.integration.id, ])) } From 4649782ff02746d3c5c9cef93585a05bf816a8bb Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 13:27:23 +0700 Subject: [PATCH 8/9] i162: keep it simple --- .../api-gateway.tf | 23 +------------------ 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/tutorial/serverless-basic-application/api-gateway.tf b/tutorial/serverless-basic-application/api-gateway.tf index ceedaf7..6a443b6 100644 --- a/tutorial/serverless-basic-application/api-gateway.tf +++ b/tutorial/serverless-basic-application/api-gateway.tf @@ -12,23 +12,7 @@ resource "aws_api_gateway_stage" "lambda" { stage_name = "serverless_lambda_stage" # Temporarity disable logging. TODO: work on it later - # 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" - # } - # ) - # } + # access_log_settings { ... } } # Resource @@ -76,11 +60,6 @@ resource "aws_api_gateway_deployment" "example" { # Logging # Temporarity disable logging. TODO: work on it later -# resource "aws_cloudwatch_log_group" "api_gw" { -# name = "/aws/api_gw/${aws_api_gateway_rest_api.lambda.name}" - -# retention_in_days = 30 -# } resource "aws_lambda_permission" "api_gw" { statement_id = "AllowExecutionFromAPIGateway" From bd9b6ab4f2a042d73d26bbffac1742de55d086ec Mon Sep 17 00:00:00 2001 From: Tung Bui Date: Sat, 1 Jul 2023 13:33:15 +0700 Subject: [PATCH 9/9] i162 - cleanup resource --- .../api-gateway.tf | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/tutorial/serverless-basic-application/api-gateway.tf b/tutorial/serverless-basic-application/api-gateway.tf index 6a443b6..e0e3514 100644 --- a/tutorial/serverless-basic-application/api-gateway.tf +++ b/tutorial/serverless-basic-application/api-gateway.tf @@ -1,20 +1,10 @@ -# Flow: New API -> Resource -> Method -> Interation -> Deployment +# Flow: +# New API -> Resource -> Method -> Interation -> Deployment resource "aws_api_gateway_rest_api" "lambda" { name = "serverless_lambda_gw" } -# Stage -resource "aws_api_gateway_stage" "lambda" { - rest_api_id = aws_api_gateway_rest_api.lambda.id - deployment_id = aws_api_gateway_deployment.example.id - - stage_name = "serverless_lambda_stage" - - # Temporarity disable logging. TODO: work on it later - # access_log_settings { ... } -} - # Resource resource "aws_api_gateway_resource" "resource" { path_part = "resource" @@ -45,7 +35,6 @@ resource "aws_api_gateway_deployment" "example" { rest_api_id = aws_api_gateway_rest_api.lambda.id triggers = { - # redeployment = sha1(jsonencode(aws_api_gateway_rest_api.lambda.body)) redeployment = sha1(jsonencode([ aws_api_gateway_resource.resource.id, aws_api_gateway_method.method.id, @@ -58,14 +47,15 @@ resource "aws_api_gateway_deployment" "example" { } } -# Logging -# Temporarity disable logging. TODO: work on it later - -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" +# Stage +resource "aws_api_gateway_stage" "lambda" { + rest_api_id = aws_api_gateway_rest_api.lambda.id + deployment_id = aws_api_gateway_deployment.example.id + stage_name = "serverless_lambda_stage" - source_arn = "${aws_api_gateway_rest_api.lambda.execution_arn}/*/*" + # Temporarity disable logging. TODO: work on it later + # access_log_settings { ... } } + +# Logging +# Temporarity disable logging. TODO: work on it later