|
| 1 | +resource "aws_apigatewayv2_api" "api_gw" { |
| 2 | + name = aws_lambda_function.lambda.function_name |
| 3 | + protocol_type = "HTTP" |
| 4 | + description = "HTTP API for ${aws_lambda_function.lambda.function_name}" |
| 5 | + |
| 6 | + cors_configuration { |
| 7 | + allow_credentials = false |
| 8 | + allow_headers = [] |
| 9 | + allow_methods = [var.http_method] |
| 10 | + allow_origins = ["*"] |
| 11 | + expose_headers = [] |
| 12 | + max_age = 0 |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +resource "aws_apigatewayv2_stage" "default" { |
| 17 | + api_id = aws_apigatewayv2_api.api_gw.id |
| 18 | + |
| 19 | + name = "$default" |
| 20 | + auto_deploy = true |
| 21 | + |
| 22 | + access_log_settings { |
| 23 | + destination_arn = aws_cloudwatch_log_group.api_gw.arn |
| 24 | + |
| 25 | + format = jsonencode({ |
| 26 | + request_id = "$context.requestId" |
| 27 | + source_ip = "$context.identity.sourceIp" |
| 28 | + request_time = "$context.requestTime" |
| 29 | + protocol = "$context.protocol" |
| 30 | + http_method = "$context.httpMethod" |
| 31 | + resource_path = "$context.resourcePath" |
| 32 | + route_key = "$context.routeKey" |
| 33 | + status = "$context.status" |
| 34 | + response_length = "$context.responseLength" |
| 35 | + integration_error_message = "$context.integrationErrorMessage" |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + default_route_settings { |
| 40 | + throttling_burst_limit = var.burst_limit_rps |
| 41 | + throttling_rate_limit = var.rate_limit_rps |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +resource "aws_apigatewayv2_integration" "lambda" { |
| 46 | + api_id = aws_apigatewayv2_api.api_gw.id |
| 47 | + |
| 48 | + integration_uri = aws_lambda_function.lambda.invoke_arn |
| 49 | + integration_type = "AWS_PROXY" |
| 50 | + payload_format_version = "2.0" |
| 51 | + timeout_milliseconds = 1000 * local.http_timeout_in_seconds |
| 52 | +} |
| 53 | + |
| 54 | +resource "aws_apigatewayv2_route" "lambda" { |
| 55 | + api_id = aws_apigatewayv2_api.api_gw.id |
| 56 | + route_key = "${var.http_method} ${var.http_route}" |
| 57 | + target = "integrations/${aws_apigatewayv2_integration.lambda.id}" |
| 58 | +} |
| 59 | + |
| 60 | +resource "aws_lambda_permission" "api_gw" { |
| 61 | + statement_id = "AllowExecutionFromAPIGateway" |
| 62 | + action = "lambda:InvokeFunction" |
| 63 | + function_name = aws_lambda_function.lambda.function_name |
| 64 | + principal = "apigateway.amazonaws.com" |
| 65 | + source_arn = "${aws_apigatewayv2_api.api_gw.execution_arn}/*/*" |
| 66 | +} |
0 commit comments