From 8b63c15654006b0ff499e33a6f49a2b12331623a Mon Sep 17 00:00:00 2001 From: xpk Date: Mon, 4 Mar 2024 14:12:59 +0800 Subject: [PATCH] UPD: added multiple stage support --- .../apigw-lambda/examples/main.tf | 15 +++++++++- .../apigw-lambda/main.tf | 30 +++++++++++++++++-- .../apigw-lambda/variables.tf | 11 +++++-- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/modules/ApplicationIntegration/apigw-lambda/examples/main.tf b/modules/ApplicationIntegration/apigw-lambda/examples/main.tf index a4dc147..cbedab0 100644 --- a/modules/ApplicationIntegration/apigw-lambda/examples/main.tf +++ b/modules/ApplicationIntegration/apigw-lambda/examples/main.tf @@ -9,8 +9,21 @@ module "apigw" { lambda-archive-file = "${path.module}/lambda_function.zip" name = "ken2026-test" path_part = "hello" - stage-name = "dev" lambda-main-function-name = "main" + stages = { + "dev" : { + "description" : "Dev stage" + "variables" : { + "var1" : "foo" + } + } + "prd" : { + "description" : "Prd stage" + "variables" : { + "var1" : "bar" + } + } + } } /* diff --git a/modules/ApplicationIntegration/apigw-lambda/main.tf b/modules/ApplicationIntegration/apigw-lambda/main.tf index 7e1f4b6..1e5d4a9 100644 --- a/modules/ApplicationIntegration/apigw-lambda/main.tf +++ b/modules/ApplicationIntegration/apigw-lambda/main.tf @@ -65,15 +65,38 @@ resource "aws_api_gateway_deployment" "apigw-deployment" { } resource "aws_api_gateway_stage" "apigw-stage" { + for_each = var.stages depends_on = [aws_cloudwatch_log_group.this] deployment_id = aws_api_gateway_deployment.apigw-deployment.id rest_api_id = aws_api_gateway_rest_api.api.id - stage_name = var.stage-name + stage_name = each.key + description = each.value["description"] + variables = each.value["variables"] + + access_log_settings { + destination_arn = aws_cloudwatch_log_group.this[each.key].arn + # https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html + format = jsonencode({ + "requestId" : "$context.requestId", + "extendedRequestId" : "$context.extendedRequestId", + "ip" : "$context.identity.sourceIp", + "caller" : "$context.identity.caller", + "user" : "$context.identity.user", + "requestTime" : "$context.requestTime", + "httpMethod" : "$context.httpMethod", + "resourcePath" : "$context.resourcePath", + "status" : "$context.status", + "protocol" : "$context.protocol", + "responseLength" : "$context.responseLength" + } + ) + } } resource "aws_api_gateway_method_settings" "apigw-method-settings" { + for_each = aws_api_gateway_stage.apigw-stage rest_api_id = aws_api_gateway_rest_api.api.id - stage_name = aws_api_gateway_stage.apigw-stage.stage_name + stage_name = each.value.stage_name method_path = "*/*" settings { @@ -84,7 +107,8 @@ resource "aws_api_gateway_method_settings" "apigw-method-settings" { # Cloudwatch log group path: API-Gateway-Execution-Logs_{rest-api-id}/{stage_name} resource "aws_cloudwatch_log_group" "this" { - name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.api.id}/${var.stage-name}" + for_each = var.stages + name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.api.id}/${each.key}" retention_in_days = var.cloudwatchlog-retention kms_key_id = var.cwl-cmk-key-id } diff --git a/modules/ApplicationIntegration/apigw-lambda/variables.tf b/modules/ApplicationIntegration/apigw-lambda/variables.tf index a7f8cda..feb1fc7 100644 --- a/modules/ApplicationIntegration/apigw-lambda/variables.tf +++ b/modules/ApplicationIntegration/apigw-lambda/variables.tf @@ -13,9 +13,14 @@ variable "path_part" { description = "Last path segment of this API resource" } -variable "stage-name" { - type = string - description = "Apigateway stage name" +#variable "stage-name" { +# type = string +# description = "Apigateway stage name" +#} + +variable stages { + type = map + description = "apigateway stages" } variable "lambda-archive-file" {