-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
280 lines (228 loc) · 8.92 KB
/
main.tf
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
locals {
use_authorizer = var.authorizer != null
# To support earlier implementations, we still allow the use of `source_vpce`,
# even though we should be able to whitelist multiple source vpc endpoints.
# Therefore, we create a list of the combination of the two properties.
source_vpc_endpoints = distinct(concat(compact([var.source_vpce]), var.source_vpc_endpoints))
}
data "aws_iam_policy_document" "this" {
statement {
actions = ["execute-api:Invoke"]
resources = ["${aws_api_gateway_rest_api.this.execution_arn}/*"]
principals {
type = "*"
identifiers = ["*"]
}
}
dynamic "statement" {
for_each = length(var.ip_whitelist) != 0 && length(local.source_vpc_endpoints) == 0 ? toset([1]) : toset([])
content {
actions = ["execute-api:Invoke"]
effect = "Deny"
resources = ["${aws_api_gateway_rest_api.this.execution_arn}/*"]
condition {
test = "NotIpAddress"
variable = "aws:SourceIp"
values = var.ip_whitelist
}
principals {
type = "*"
identifiers = ["*"]
}
}
}
dynamic "statement" {
for_each = length(var.ip_whitelist) != 0 && length(local.source_vpc_endpoints) != 0 ? toset([1]) : toset([])
content {
actions = ["execute-api:Invoke"]
effect = "Deny"
resources = ["${aws_api_gateway_rest_api.this.execution_arn}/*"]
condition {
test = "NotIpAddress"
variable = "aws:VpcSourceIp"
values = var.ip_whitelist
}
principals {
type = "*"
identifiers = ["*"]
}
}
}
dynamic "statement" {
for_each = length(local.source_vpc_endpoints) != 0 ? toset([1]) : toset([])
content {
actions = ["execute-api:Invoke"]
effect = "Deny"
resources = ["${aws_api_gateway_rest_api.this.execution_arn}/*"]
condition {
test = "StringNotEquals"
variable = "aws:SourceVpce"
values = local.source_vpc_endpoints
}
principals {
type = "*"
identifiers = ["*"]
}
}
}
}
resource "aws_cloudwatch_log_group" "access" {
# checkov:skip=CKV_AWS_158: Not encrypted.
name = "/api-gw/${var.name}"
retention_in_days = var.log_retention_days
}
resource "aws_cloudwatch_log_group" "exec" {
# checkov:skip=CKV_AWS_158: Not encrypted.
name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.this.id}/${var.stage_name}"
retention_in_days = var.log_retention_days
}
resource "aws_api_gateway_rest_api" "this" {
description = var.description
name = var.name
binary_media_types = var.binary_media_types
endpoint_configuration {
types = [var.endpoint_type]
vpc_endpoint_ids = var.associate_vpc_endpoints
}
}
resource "aws_api_gateway_rest_api_policy" "this" {
rest_api_id = aws_api_gateway_rest_api.this.id
policy = data.aws_iam_policy_document.this.json
}
resource "aws_api_gateway_method_settings" "s_all" {
method_path = "*/*"
rest_api_id = aws_api_gateway_rest_api.this.id
stage_name = aws_api_gateway_stage.this.stage_name
settings {
cache_data_encrypted = var.method_settings.cache_data_encrypted
cache_ttl_in_seconds = var.method_settings.cache_ttl_in_seconds
caching_enabled = var.method_settings.caching_enabled
data_trace_enabled = var.method_settings.data_trace_enabled
logging_level = var.method_settings.logging_level
metrics_enabled = var.method_settings.metrics_enabled
require_authorization_for_cache_control = var.method_settings.require_authorization_for_cache_control
throttling_burst_limit = try(coalesce(var.method_settings.throttling_burst_limit, var.throttling_burst_limit), null)
throttling_rate_limit = try(coalesce(var.method_settings.throttling_rate_limit, var.throttling_rate_limit), null)
unauthorized_cache_control_header_strategy = var.method_settings.unauthorized_cache_control_header_strategy
}
}
resource "aws_api_gateway_stage" "this" {
# checkov:skip=CKV_AWS_120: Caching not enabled.
# checkov:skip=CKV2_AWS_4: Logging is enabled; bug maybe.
# checkov:skip=CKV2_AWS_29: WAF added outside module.
stage_name = var.stage_name
rest_api_id = aws_api_gateway_rest_api.this.id
deployment_id = aws_api_gateway_deployment.this.id
xray_tracing_enabled = var.xray_tracing_enabled
access_log_settings {
destination_arn = aws_cloudwatch_log_group.access.arn
format = jsonencode(var.access_log_format)
}
depends_on = [
aws_cloudwatch_log_group.access,
aws_cloudwatch_log_group.exec
]
}
resource "aws_api_gateway_deployment" "this" {
rest_api_id = aws_api_gateway_rest_api.this.id
triggers = {
redeployment = sha1(jsonencode([
data.aws_iam_policy_document.this.json,
aws_api_gateway_resource.depth_0,
aws_api_gateway_resource.depth_1,
aws_api_gateway_resource.depth_2,
aws_api_gateway_resource.depth_3,
aws_api_gateway_resource.depth_4,
aws_api_gateway_method.this,
aws_api_gateway_integration.this,
aws_api_gateway_integration_response.this,
aws_api_gateway_method_response.this,
var.authorizer,
var.method_settings
]))
}
lifecycle {
create_before_destroy = true
}
depends_on = [
aws_api_gateway_method.this,
aws_api_gateway_integration.this,
aws_api_gateway_integration_response.this,
aws_api_gateway_method_response.this,
aws_api_gateway_rest_api_policy.this,
]
}
resource "aws_api_gateway_domain_name" "this" {
for_each = toset(concat(try([coalesce(var.domain_name)], []), var.domain_names_alternate))
domain_name = each.key
regional_certificate_arn = var.certificate_arn
endpoint_configuration {
types = [contains(["EDGE", "REGIONAL"], var.endpoint_type) ? var.endpoint_type : "REGIONAL"]
}
}
resource "aws_api_gateway_base_path_mapping" "this" {
for_each = toset(concat(try([coalesce(var.domain_name)], []), var.domain_names_alternate))
api_id = aws_api_gateway_rest_api.this.id
stage_name = aws_api_gateway_stage.this.stage_name
domain_name = aws_api_gateway_domain_name.this[each.key].domain_name
}
resource "aws_route53_record" "this" {
count = var.domain_name != null && var.zone_id != null ? 1 : 0
name = aws_api_gateway_domain_name.this[var.domain_name].domain_name
set_identifier = try(var.routing_policy.set_identifier, null)
type = "A"
zone_id = var.zone_id
alias {
evaluate_target_health = true
name = aws_api_gateway_domain_name.this[var.domain_name].regional_domain_name
zone_id = aws_api_gateway_domain_name.this[var.domain_name].regional_zone_id
}
dynamic "cidr_routing_policy" {
for_each = var.routing_policy != null ? var.routing_policy.cidr != null ? [var.routing_policy.cidr] : [] : []
content {
collection_id = cidr_routing_policy.value.collection_id
location_name = cidr_routing_policy.value.location_name
}
}
dynamic "failover_routing_policy" {
for_each = var.routing_policy != null ? var.routing_policy.failover != null ? [var.routing_policy.failover] : [] : []
content {
type = failover_routing_policy.value.type
}
}
dynamic "geolocation_routing_policy" {
for_each = var.routing_policy != null ? var.routing_policy.geolocation != null ? [var.routing_policy.geolocation] : [] : []
content {
continent = geolocation_routing_policy.value.continent
country = geolocation_routing_policy.value.country
subdivision = geolocation_routing_policy.value.subdivision
}
}
dynamic "geoproximity_routing_policy" {
for_each = var.routing_policy != null ? var.routing_policy.geoproximity != null ? [var.routing_policy.geoproximity] : [] : []
content {
aws_region = geoproximity_routing_policy.value.aws_region
bias = geoproximity_routing_policy.value.bias
local_zone_group = geoproximity_routing_policy.value.local_zone_group
dynamic "coordinates" {
for_each = geoproximity_routing_policy.value.coordinates != null ? [geoproximity_routing_policy.value.coordinates] : []
content {
latitude = coordinates.value.latitude
longitude = coordinates.value.longitude
}
}
}
}
dynamic "latency_routing_policy" {
for_each = var.routing_policy != null ? var.routing_policy.latency != null ? [var.routing_policy.latency] : [] : []
content {
region = latency_routing_policy.value.region
}
}
dynamic "weighted_routing_policy" {
for_each = var.routing_policy != null ? var.routing_policy.weighted != null ? [var.routing_policy.weighted] : [] : []
content {
weight = weighted_routing_policy.value.weight
}
}
}