-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
executable file
·402 lines (325 loc) · 14.5 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
data "aws_partition" "this" {}
data "aws_caller_identity" "this" {}
resource "aws_cloudfront_cache_policy" "this" {
for_each = var.cache_policies
name = "${local.environment}-${each.key}-cache-policy"
comment = "Cache policy"
default_ttl = each.value.default_ttl
max_ttl = each.value.max_ttl
min_ttl = each.value.min_ttl
parameters_in_cache_key_and_forwarded_to_origin {
cookies_config {
cookie_behavior = each.value.cookies_config.cookie_behavior
cookies {
items = each.value.cookies_config.items
}
}
headers_config {
header_behavior = each.value.headers_config.header_behavior
headers {
items = each.value.headers_config.items
}
}
query_strings_config {
query_string_behavior = each.value.query_strings_config.query_string_behavior
query_strings {
items = each.value.query_strings_config.items
}
}
}
}
resource "aws_cloudfront_origin_request_policy" "this" {
for_each = var.origin_request_policies
name = "${local.environment}-${each.key}-origin-request-policy"
comment = "Origin request policy"
cookies_config {
cookie_behavior = each.value.cookies_config.cookie_behavior
cookies {
items = each.value.cookies_config.items
}
}
headers_config {
header_behavior = each.value.headers_config.header_behavior
headers {
items = each.value.headers_config.items
}
}
query_strings_config {
query_string_behavior = each.value.query_strings_config.query_string_behavior
query_strings {
items = each.value.query_strings_config.items
}
}
}
resource "aws_cloudfront_response_headers_policy" "this" {
for_each = var.response_headers_policy
name = each.value.name
comment = each.value.comment
dynamic "cors_config" {
for_each = each.value.cors_config == null ? [] : [1]
content {
access_control_allow_credentials = try(each.value.cors_config.access_control_allow_credentials, false)
access_control_allow_headers {
items = try(each.value.cors_config.access_control_allow_headers.items, [])
}
access_control_allow_methods {
items = try(each.value.cors_config.access_control_allow_methods.items, [])
}
access_control_allow_origins {
items = try(each.value.cors_config.access_control_allow_origins.items, [])
}
access_control_expose_headers {
items = try(each.value.cors_config.access_control_expose_headers.items, [])
}
access_control_max_age_sec = try(each.value.cors_config.access_control_max_age_sec, 600)
origin_override = try(each.value.cors_config.origin_override, true)
}
}
dynamic "security_headers_config" {
for_each = each.value.security_headers_config == null ? [] : [1]
content {
content_type_options {
override = try(each.value.security_headers_config.content_type_options.override, false)
}
frame_options {
frame_option = each.value.security_headers_config.frame_options.frame_option
override = try(each.value.security_headers_config.frame_options.override, false)
}
referrer_policy {
referrer_policy = each.value.security_headers_config.referrer_policy.referrer_policy
override = try(each.value.security_headers_config.referrer_policy.override, false)
}
xss_protection {
mode_block = try(each.value.security_headers_config.xss_protection.mode_block, false)
protection = try(each.value.security_headers_config.xss_protection.protection, false)
override = try(each.value.security_headers_config.xss_protection.override, false)
report_uri = try(each.value.security_headers_config.xss_protection.report_uri, "")
}
strict_transport_security {
access_control_max_age_sec = try(each.value.security_headers_config.strict_transport_security.access_control_max_age_sec, "31536000")
include_subdomains = try(each.value.security_headers_config.strict_transport_security.include_subdomains, false)
preload = try(each.value.security_headers_config.strict_transport_security.preload, false)
override = try(each.value.security_headers_config.strict_transport_security.override, false)
}
content_security_policy {
content_security_policy = try(each.value.security_headers_config.content_security_policy.content_security_policy, "31536000")
override = try(each.value.security_headers_config.content_security_policy.override, false)
}
}
}
dynamic "server_timing_headers_config" {
for_each = each.value.server_timing_headers_config == null ? [] : [1]
content {
enabled = try(each.value.server_timing_headers_config.enabled, false)
sampling_rate = try(each.value.server_timing_headers_config.sampling_rate, 0)
}
}
# TODO: Fix issue in setting below configs
# dynamic "remove_headers_config" {
# for_each = each.value.remove_headers_config == null ? [] : [each.value.remove_headers_config]
# content {
# dynamic "items" {
# for_each = remove_headers_config
# content {
# header = items
# }
# }
# }
# }
# dynamic "custom_headers_config" {
# for_each = each.value.custom_headers_config == null ? [] : [each.value.custom_headers_config]
# content {
# dynamic "items" {
# for_each = custom_headers_config
# content {
# header = items.header
# override = try(items.override, false)
# value = try(items.value, "none")
# }
# }
# }
# }
}
resource "aws_s3_bucket_policy" "cdn_bucket_policy" {
for_each = {
for index, origin in var.origins : origin.origin_id => origin
if origin.origin_type == "s3"
}
bucket = each.value.create_bucket ? module.s3_bucket[each.value.origin_id].bucket_id : data.aws_s3_bucket.origin[each.value.origin_id].id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "AllowCloudFrontServicePrincipal"
Effect = "Allow"
Principal = {
Service = "cloudfront.amazonaws.com"
}
Action = "s3:GetObject"
Resource = each.value.create_bucket ? "${module.s3_bucket[each.value.origin_id].bucket_arn}/*" : "${data.aws_s3_bucket.origin[each.value.origin_id].arn}/*"
Condition = {
StringEquals = {
"aws:SourceArn" = aws_cloudfront_distribution.this.arn
}
}
}
]
})
}
##################################################################################
# CDN #
##################################################################################
resource "aws_cloudfront_origin_access_control" "s3" {
for_each = {
for index, origin in var.origins : origin.origin_id => origin
if origin.origin_type == "s3"
}
name = each.value.origin_id
description = "Origin access control"
origin_access_control_origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
resource "aws_cloudfront_distribution" "this" {
dynamic "origin" {
for_each = {
for index, origin in var.origins :
origin.origin_id => origin
}
iterator = i
content {
domain_name = i.value.origin_type == "s3" ? (i.value.create_bucket ? module.s3_bucket[i.value.origin_id].bucket_regional_domain_name : data.aws_s3_bucket.origin[i.value.origin_id].bucket_regional_domain_name) : i.value.domain_name
origin_id = i.value.origin_id
origin_access_control_id = i.value.origin_type == "s3" ? aws_cloudfront_origin_access_control.s3[i.value.origin_id].id : null
connection_attempts = i.value.connection_attempts
connection_timeout = i.value.connection_timeout
dynamic "custom_origin_config" {
for_each = i.value.custom_origin_config == null ? [] : [1]
content {
http_port = i.value.custom_origin_config.http_port
https_port = i.value.custom_origin_config.https_port
origin_protocol_policy = i.value.custom_origin_config.origin_protocol_policy
origin_ssl_protocols = i.value.custom_origin_config.origin_ssl_protocols
origin_keepalive_timeout = i.value.custom_origin_config.origin_keepalive_timeout
origin_read_timeout = i.value.custom_origin_config.origin_read_timeout
}
}
dynamic "origin_shield" {
for_each = try(i.value.custom_origin_config.enabled, null) == null || try(i.value.custom_origin_config.enabled, null) == false ? [] : [1]
content {
enabled = try(i.value.custom_origin_config.enabled, null)
origin_shield_region = try(i.value.custom_origin_config.origin_shield_region, null)
}
}
}
}
comment = var.description
enabled = true
is_ipv6_enabled = true
default_root_object = var.default_root_object
price_class = var.price_class
dynamic "custom_error_response" {
for_each = {
for index, err_resp in var.custom_error_responses :
err_resp.error_code => err_resp
}
iterator = i
content {
error_caching_min_ttl = lookup(i.value, "error_caching_min_ttl", 0)
error_code = i.value.error_code
response_code = lookup(i.value, "response_code", "")
response_page_path = lookup(i.value, "response_page_path", "")
}
}
default_cache_behavior {
allowed_methods = var.default_cache_behavior.allowed_methods
cached_methods = var.default_cache_behavior.cached_methods
target_origin_id = var.default_cache_behavior.origin_id
compress = lookup(var.default_cache_behavior, "compress", false)
viewer_protocol_policy = var.default_cache_behavior.viewer_protocol_policy
cache_policy_id = var.default_cache_behavior.use_aws_managed_cache_policy ? local.managed_cache_policies[var.default_cache_behavior.cache_policy_name] : aws_cloudfront_cache_policy.this[var.default_cache_behavior.cache_policy_name].id
origin_request_policy_id = var.default_cache_behavior.use_aws_managed_origin_request_policy ? local.managed_origin_request_policies[var.default_cache_behavior.origin_request_policy_name] : (var.default_cache_behavior.origin_request_policy_name == null ? null : aws_cloudfront_origin_request_policy.this[var.default_cache_behavior.origin_request_policy_name].id)
response_headers_policy_id = var.default_cache_behavior.use_aws_managed_response_headers_policy ? local.managed_response_header_policies[var.default_cache_behavior.response_headers_policy_name] : (var.default_cache_behavior.response_headers_policy_name == null ? null : aws_cloudfront_response_headers_policy.this[var.default_cache_behavior.response_headers_policy_name].id)
dynamic "lambda_function_association" {
for_each = var.default_cache_behavior.lambda_function_association == null ? [] : var.default_cache_behavior.lambda_function_association
iterator = i
content {
event_type = i.value.event_type
lambda_arn = i.value.lambda_arn
include_body = i.value.include_body
}
}
dynamic "function_association" {
for_each = var.default_cache_behavior.function_association == null ? [] : var.default_cache_behavior.function_association
iterator = i
content {
event_type = i.value.event_type
function_arn = i.value.function_arn
}
}
}
dynamic "ordered_cache_behavior" {
for_each = {
for index, cache in var.cache_behaviors :
cache.path_pattern => cache
}
iterator = i
content {
path_pattern = i.value.path_pattern
allowed_methods = i.value.allowed_methods
cached_methods = i.value.cached_methods
target_origin_id = i.value.origin_id
compress = lookup(i.value, "compress", false)
viewer_protocol_policy = i.value.viewer_protocol_policy
cache_policy_id = i.value.use_aws_managed_cache_policy ? local.managed_cache_policies[i.value.cache_policy_name] : aws_cloudfront_cache_policy.this[i.value.cache_policy_name].id
origin_request_policy_id = i.value.use_aws_managed_origin_request_policy ? local.managed_origin_request_policies[i.value.origin_request_policy_name] : (i.value.origin_request_policy_name == null ? null : aws_cloudfront_origin_request_policy.this[i.value.origin_request_policy_name].id)
response_headers_policy_id = i.value.use_aws_managed_response_headers_policy ? local.managed_response_header_policies[i.value.response_headers_policy_name] : (i.value.response_headers_policy_name == null ? null : aws_cloudfront_response_headers_policy.this[i.value.response_headers_policy_name].id)
dynamic "lambda_function_association" {
for_each = i.value.lambda_function_association == null ? {} : {
for index, function in i.value.lambda_function_association :
function.event_type => function
}
iterator = j
content {
event_type = j.value.event_type
lambda_arn = j.value.lambda_arn
include_body = j.value.include_body
}
}
dynamic "function_association" {
for_each = i.value.function_association == null ? {} : {
for index, function in i.value.function_association :
function.event_type => function
}
iterator = j
content {
event_type = j.value.event_type
function_arn = j.value.function_arn
}
}
}
}
aliases = var.aliases
restrictions {
geo_restriction {
restriction_type = var.geo_restriction.restriction_type
locations = var.geo_restriction.locations
}
}
dynamic "logging_config" {
for_each = var.enable_logging ? [1] : []
content {
include_cookies = false
bucket = module.s3_bucket_logs[0].bucket_domain_name
prefix = local.environment
}
}
viewer_certificate {
acm_certificate_arn = var.viewer_certificate.cloudfront_default_certificate ? null : aws_acm_certificate.this[0].arn
cloudfront_default_certificate = var.viewer_certificate.cloudfront_default_certificate
minimum_protocol_version = var.viewer_certificate.minimum_protocol_version
ssl_support_method = var.viewer_certificate.ssl_support_method
}
web_acl_id = var.web_acl_id
retain_on_delete = var.retain_on_delete
}