1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100locals {
name = "${var.project_name}-${var.environment}-api"
}
resource "aws_apigatewayv2_api" "this" {
name = local.name
protocol_type = "HTTP"
cors_configuration {
allow_origins = var.cors_allow_origins
allow_methods = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
allow_headers = ["Content-Type", "Authorization", "x-hasura-admin-secret"]
}
}
resource "aws_cloudwatch_log_group" "access_logs" {
name = "/aws/apigateway/${local.name}"
retention_in_days = var.log_retention_in_days
}
resource "aws_apigatewayv2_stage" "default" {
api_id = aws_apigatewayv2_api.this.id
name = "$default"
auto_deploy = true
access_log_settings {
destination_arn = aws_cloudwatch_log_group.access_logs.arn
format = jsonencode({
requestId = "$context.requestId"
ip = "$context.identity.sourceIp"
requestTime = "$context.requestTime"
httpMethod = "$context.httpMethod"
routeKey = "$context.routeKey"
status = "$context.status"
responseLength = "$context.responseLength"
integrationErrorMessage = "$context.integrationErrorMessage"
})
}
}
resource "aws_apigatewayv2_integration" "backend" {
api_id = aws_apigatewayv2_api.this.id
integration_type = "AWS_PROXY"
integration_uri = var.backend_lambda_invoke_arn
payload_format_version = "2.0"
}
# Matches apps/backend/index.ts's own path-based router — every real method
# forwards to the backend Lambda. Deliberately NOT "ANY", which also matches
# OPTIONS: that would route CORS preflight requests to the Lambda itself
# (which has no preflight handling and 401s without an Authorization header)
# instead of letting API Gateway's own cors_configuration auto-answer them.
locals {
proxy_methods = toset(["GET", "POST", "PUT", "PATCH", "DELETE"])
}
resource "aws_apigatewayv2_route" "proxy" {
for_each = local.proxy_methods
api_id = aws_apigatewayv2_api.this.id
route_key = "${each.value} /{proxy+}"
target = "integrations/${aws_apigatewayv2_integration.backend.id}"
}
resource "aws_apigatewayv2_route" "root" {
for_each = local.proxy_methods
api_id = aws_apigatewayv2_api.this.id
route_key = "${each.value} /"
target = "integrations/${aws_apigatewayv2_integration.backend.id}"
}
resource "aws_lambda_permission" "apigw" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = var.backend_lambda_function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.this.execution_arn}/*/*"
}
resource "aws_apigatewayv2_domain_name" "this" {
count = var.domain_name != null ? 1 : 0
domain_name = var.domain_name
domain_name_configuration {
certificate_arn = var.acm_certificate_arn
endpoint_type = "REGIONAL"
security_policy = "TLS_1_2"
}
}
resource "aws_apigatewayv2_api_mapping" "this" {
count = var.domain_name != null ? 1 : 0
api_id = aws_apigatewayv2_api.this.id
domain_name = aws_apigatewayv2_domain_name.this[0].id
stage = aws_apigatewayv2_stage.default.id
}