|
| 1 | +resource "aws_apigatewayv2_api" "health_api" { |
| 2 | + name = "${var.environment}-serverless-health-check-api" |
| 3 | + protocol_type = "HTTP" |
| 4 | + |
| 5 | + cors_configuration { |
| 6 | + allow_origins = ["*"] |
| 7 | + allow_methods = ["GET", "POST", "OPTIONS"] |
| 8 | + allow_headers = ["*"] |
| 9 | + } |
| 10 | + |
| 11 | + tags = var.common_tags |
| 12 | +} |
| 13 | + |
| 14 | +resource "aws_apigatewayv2_integration" "lambda_integration" { |
| 15 | + api_id = aws_apigatewayv2_api.health_api.id |
| 16 | + integration_type = "AWS_PROXY" |
| 17 | + integration_method = "POST" |
| 18 | + payload_format_version = "2.0" |
| 19 | + integration_uri = var.lambda_invoke_arn |
| 20 | + depends_on = [] |
| 21 | +} |
| 22 | + |
| 23 | +resource "aws_apigatewayv2_route" "health_route_get" { |
| 24 | + api_id = aws_apigatewayv2_api.health_api.id |
| 25 | + route_key = "GET /health" |
| 26 | + target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}" |
| 27 | +} |
| 28 | + |
| 29 | +resource "aws_apigatewayv2_route" "health_route_post" { |
| 30 | + api_id = aws_apigatewayv2_api.health_api.id |
| 31 | + route_key = "POST /health" |
| 32 | + target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}" |
| 33 | +} |
| 34 | + |
| 35 | +resource "aws_apigatewayv2_stage" "default" { |
| 36 | + api_id = aws_apigatewayv2_api.health_api.id |
| 37 | + name = "$default" |
| 38 | + auto_deploy = true |
| 39 | + |
| 40 | + access_log_settings { |
| 41 | + destination_arn = aws_cloudwatch_log_group.api_gateway_logs.arn |
| 42 | + format = jsonencode({ |
| 43 | + requestId = "$context.requestId" |
| 44 | + ip = "$context.identity.sourceIp" |
| 45 | + requestTime = "$context.requestTime" |
| 46 | + httpMethod = "$context.httpMethod" |
| 47 | + resourcePath = "$context.resourcePath" |
| 48 | + status = "$context.status" |
| 49 | + protocol = "$context.protocol" |
| 50 | + responseLength = "$context.responseLength" |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + tags = var.common_tags |
| 55 | +} |
| 56 | + |
| 57 | +resource "aws_cloudwatch_log_group" "api_gateway_logs" { |
| 58 | + name = "/aws/apigateway/${var.environment}-serverless-health-check-api" |
| 59 | + retention_in_days = 7 |
| 60 | + |
| 61 | + tags = var.common_tags |
| 62 | +} |
0 commit comments