|
| 1 | +# ############################################################################## |
| 2 | +# API Gateway HTTP API (v2) |
| 3 | +# ############################################################################## |
| 4 | + |
| 5 | +resource "aws_apigatewayv2_api" "openfactcheck" { |
| 6 | + name = "openfactcheck-api-${terraform.workspace}-${var.aws_region}" |
| 7 | + description = "OpenFactCheck API Gateway for ${terraform.workspace}-${var.aws_region}" |
| 8 | + protocol_type = "HTTP" |
| 9 | + |
| 10 | + disable_execute_api_endpoint = true |
| 11 | + |
| 12 | + cors_configuration { |
| 13 | + allow_origins = var.cors_origins |
| 14 | + allow_methods = ["GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"] |
| 15 | + allow_headers = ["Authorization", "Content-Type", "X-Request-ID"] |
| 16 | + allow_credentials = true |
| 17 | + max_age = 3600 |
| 18 | + } |
| 19 | + |
| 20 | + tags = { |
| 21 | + Name = "OpenFactCheck - API Gateway - ${terraform.workspace} - ${var.aws_region}" |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +# ############################################################################## |
| 26 | +# Integration — Lambda proxy via LIVE alias |
| 27 | +# ############################################################################## |
| 28 | + |
| 29 | +resource "aws_apigatewayv2_integration" "lambda" { |
| 30 | + api_id = aws_apigatewayv2_api.openfactcheck.id |
| 31 | + description = "OpenFactCheck API Lambda for ${terraform.workspace}-${var.aws_region}" |
| 32 | + integration_type = "AWS_PROXY" |
| 33 | + connection_type = "INTERNET" |
| 34 | + integration_method = "POST" |
| 35 | + integration_uri = aws_lambda_alias.api_live.invoke_arn |
| 36 | + payload_format_version = "2.0" |
| 37 | +} |
| 38 | + |
| 39 | +# ############################################################################## |
| 40 | +# Routes |
| 41 | +# ############################################################################## |
| 42 | + |
| 43 | +resource "aws_apigatewayv2_route" "default" { |
| 44 | + api_id = aws_apigatewayv2_api.openfactcheck.id |
| 45 | + route_key = "$default" |
| 46 | + target = "integrations/${aws_apigatewayv2_integration.lambda.id}" |
| 47 | +} |
| 48 | + |
| 49 | +resource "aws_apigatewayv2_route" "options" { |
| 50 | + api_id = aws_apigatewayv2_api.openfactcheck.id |
| 51 | + route_key = "OPTIONS /{proxy+}" |
| 52 | + target = "integrations/${aws_apigatewayv2_integration.lambda.id}" |
| 53 | + authorization_type = "NONE" |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +# ############################################################################## |
| 58 | +# Stage — auto-deploy with access logging |
| 59 | +# ############################################################################## |
| 60 | + |
| 61 | +resource "aws_apigatewayv2_stage" "default" { |
| 62 | + api_id = aws_apigatewayv2_api.openfactcheck.id |
| 63 | + name = "$default" |
| 64 | + auto_deploy = true |
| 65 | + |
| 66 | + access_log_settings { |
| 67 | + destination_arn = aws_cloudwatch_log_group.api.arn |
| 68 | + format = <<JSON |
| 69 | + { "requestTime": "$context.requestTime", "requestId": "$context.requestId", "httpMethod": "$context.httpMethod", "path": "$context.path", "routeKey": "$context.routeKey", "status": $context.status, "responseLatency": $context.responseLatency, "integrationRequestId": "$context.integration.requestId", "functionResponseStatus": "$context.integration.status", "integrationLatency": "$context.integration.latency", "integrationServiceStatus": "$context.integration.integrationStatus", "ip": "$context.identity.sourceIp", "userAgent": "$context.identity.userAgent", "error": { "message": "$context.error.message", "responseType": "$context.error.responseType" } } |
| 70 | + JSON |
| 71 | + } |
| 72 | + |
| 73 | + tags = { |
| 74 | + Name = "OpenFactCheck - API Gateway Stage - ${terraform.workspace} - ${var.aws_region}" |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +# ############################################################################## |
| 79 | +# Custom Domain |
| 80 | +# ############################################################################## |
| 81 | + |
| 82 | +resource "aws_apigatewayv2_domain_name" "api" { |
| 83 | + domain_name = local.api_domain |
| 84 | + |
| 85 | + domain_name_configuration { |
| 86 | + certificate_arn = local.certificate_arn |
| 87 | + endpoint_type = "REGIONAL" |
| 88 | + security_policy = "TLS_1_2" |
| 89 | + } |
| 90 | + |
| 91 | + tags = { |
| 92 | + Name = "OpenFactCheck - API Domain - ${terraform.workspace} - ${var.aws_region}" |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +resource "aws_apigatewayv2_api_mapping" "api" { |
| 97 | + api_id = aws_apigatewayv2_api.openfactcheck.id |
| 98 | + domain_name = aws_apigatewayv2_domain_name.api.id |
| 99 | + stage = aws_apigatewayv2_stage.default.id |
| 100 | +} |
| 101 | + |
| 102 | +# ############################################################################## |
| 103 | +# API Gateway Logs |
| 104 | +# ############################################################################## |
| 105 | + |
| 106 | +resource "aws_cloudwatch_log_group" "api" { |
| 107 | + name = "/openfactcheck-${terraform.workspace}-${var.aws_region}/api/" |
| 108 | + retention_in_days = 30 |
| 109 | + |
| 110 | + tags = { |
| 111 | + Name = "OpenFactCheck - CloudWatch - API - ${terraform.workspace} - ${var.aws_region}" |
| 112 | + } |
| 113 | +} |
0 commit comments