Repository navigation
Expand file tree
/
Copy pathmain.tf
More file actions
151 lines (128 loc) · 4.19 KB
/
Copy pathmain.tf
File metadata and controls
151 lines (128 loc) · 4.19 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
locals {
stages = var.stages
endpoint_type = var.endpoint_type
enable_metrics = var.enable_metrics
}
resource "aws_api_gateway_rest_api" "api" {
for_each = toset(local.stages)
name = "${module.this.id}-${each.key}"
description = var.description
endpoint_configuration {
types = [
local.endpoint_type
]
}
tags = module.this.tags
}
resource "aws_api_gateway_method" "placeholder" {
for_each = toset(local.stages)
rest_api_id = aws_api_gateway_rest_api.api[each.key].id
resource_id = aws_api_gateway_rest_api.api[each.key].root_resource_id
http_method = "GET"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "placeholder" {
for_each = toset(local.stages)
rest_api_id = aws_api_gateway_rest_api.api[each.key].id
resource_id = aws_api_gateway_rest_api.api[each.key].root_resource_id
http_method = aws_api_gateway_method.placeholder[each.key].http_method
type = "MOCK"
}
resource "aws_api_gateway_deployment" "placeholder" {
for_each = toset(local.stages)
rest_api_id = aws_api_gateway_rest_api.api[each.key].id
depends_on = [
aws_api_gateway_method.placeholder,
aws_api_gateway_integration.placeholder
]
triggers = {
# Always redeploy to capture external changes (Serverless, etc.)
redeployment = timestamp()
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_api_gateway_stage" "stage" {
for_each = toset(local.stages)
stage_name = each.key
rest_api_id = aws_api_gateway_rest_api.api[each.key].id
deployment_id = aws_api_gateway_deployment.placeholder[each.key].id
dynamic "access_log_settings" {
for_each = var.access_log_destination_arn != null ? [1] : []
content {
destination_arn = var.access_log_destination_arn
format = jsonencode({
requestId = "$context.requestId"
ip = "$context.identity.sourceIp"
requestTime = "$context.requestTime"
httpMethod = "$context.httpMethod"
resourcePath = "$context.resourcePath"
status = "$context.status"
responseLength = "$context.responseLength"
latency = "$context.responseLatency"
integrationLatency = "$context.integration.latency"
errorMessage = "$context.error.message"
apiKey = "$context.identity.apiKey"
})
}
}
tags = module.this.tags
}
resource "aws_api_gateway_method_settings" "settings" {
for_each = toset(local.stages)
rest_api_id = aws_api_gateway_rest_api.api[each.key].id
stage_name = aws_api_gateway_stage.stage[each.key].stage_name
method_path = "*/*"
settings {
metrics_enabled = local.enable_metrics
logging_level = var.access_log_destination_arn != null ? "INFO" : "OFF"
throttling_rate_limit = var.stage_throttle_rate_limit
throttling_burst_limit = var.stage_throttle_burst_limit
}
}
# API Keys (conditional)
resource "aws_api_gateway_api_key" "default" {
for_each = var.create_usage_plan ? toset(local.stages) : []
name = join("-", [
module.this.id,
"key",
each.key
])
tags = module.this.tags
}
# Usage Plan (conditional)
resource "aws_api_gateway_usage_plan" "default" {
count = var.create_usage_plan ? 1 : 0
name = "${module.this.id}-default-plan"
dynamic "api_stages" {
for_each = toset(local.stages)
content {
api_id = aws_api_gateway_rest_api.api[api_stages.value].id
stage = api_stages.value
}
}
dynamic "throttle_settings" {
for_each = var.throttle_rate_limit != null ? [1] : []
content {
rate_limit = var.throttle_rate_limit
burst_limit = var.throttle_burst_limit
}
}
dynamic "quota_settings" {
for_each = var.quota_limit != null ? [1] : []
content {
limit = var.quota_limit
period = var.quota_period
}
}
depends_on = [aws_api_gateway_stage.stage]
tags = module.this.tags
}
# Link API Keys to Usage Plan (conditional)
resource "aws_api_gateway_usage_plan_key" "default" {
for_each = var.create_usage_plan ? toset(local.stages) : []
key_id = aws_api_gateway_api_key.default[each.key].id
key_type = "API_KEY"
usage_plan_id = aws_api_gateway_usage_plan.default[0].id
}