-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
362 lines (300 loc) · 13.8 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
/* -------------------------------------------------------------------------- */
/* Locals */
/* -------------------------------------------------------------------------- */
locals {
name = var.name_override == "" ? format("%s-%s-%s-cf", var.prefix, var.environment, var.name) : var.name_override
aliases_records = { for name in var.domain_aliases : name => { "name" = name } }
is_use_cloudfront_cert_viewer = var.cdn_certificate_arn == null && var.is_automatic_create_dns_record == false && length(var.domain_aliases) == 0 ? true : false
tags = merge(
{
"Environment" = var.environment,
"Terraform" = "true"
"Module" = "terraform-aws-cloudfront"
},
var.tags
)
}
locals {
empty_prefix = var.prefix == "" ? true : false
empty_environment = var.environment == "" ? true : false
empty_name = var.name == "" ? true : false
raise_empty_name = local.name == "" && (local.empty_prefix || local.empty_environment || local.empty_name) ? file("`var.name_override` or (`var.prefix`, `var.environment` and `var.name is required`) ") : null
}
/* -------------------------------------------------------------------------- */
/* Route53 */
/* -------------------------------------------------------------------------- */
data "aws_route53_zone" "hosted_zone" {
count = var.is_automatic_create_dns_record ? 1 : 0
name = var.route53_domain_name
private_zone = false
}
resource "aws_route53_record" "application" {
for_each = var.is_automatic_create_dns_record ? local.aliases_records : {}
zone_id = data.aws_route53_zone.hosted_zone[0].id
name = each.value.name
type = "A"
alias {
name = lower(aws_cloudfront_distribution.distribution.domain_name)
zone_id = aws_cloudfront_distribution.distribution.hosted_zone_id
evaluate_target_health = true
}
}
/* -------------------------------------------------------------------------- */
/* Origin Access Control Indentity (OAI) */
/* -------------------------------------------------------------------------- */
resource "aws_cloudfront_origin_access_identity" "this" {
for_each = var.origin_access_identities
comment = each.value
lifecycle {
create_before_destroy = true
}
}
/* -------------------------------------------------------------------------- */
/* Distribution */
/* -------------------------------------------------------------------------- */
resource "aws_cloudfront_distribution" "distribution" {
enabled = var.is_enable_distribution
comment = local.name
price_class = var.price_class
web_acl_id = var.is_enable_waf ? module.waf[0].web_acl_id : null
is_ipv6_enabled = var.is_ipv6_enabled
default_root_object = var.default_root_object
retain_on_delete = var.retain_on_delete
# By-default, fqdn for the CDN should be added, it should be the one for which certificate is issued
aliases = var.domain_aliases
dynamic "origin_group" {
for_each = var.origin_group
iterator = origin_group
content {
origin_id = lookup(origin_group.value, "origin_id", origin_group.key)
failover_criteria {
status_codes = origin_group.value["failover_status_codes"]
}
member {
origin_id = origin_group.value["primary_member_origin_id"]
}
member {
origin_id = origin_group.value["secondary_member_origin_id"]
}
}
}
default_cache_behavior {
allowed_methods = lookup(var.default_cache_behavior, "allowed_methods", ["GET", "HEAD", "OPTIONS"])
cached_methods = lookup(var.default_cache_behavior, "cached_methods", ["GET", "HEAD"])
target_origin_id = var.default_cache_behavior.target_origin_id
compress = lookup(var.default_cache_behavior, "compress", null)
min_ttl = lookup(var.default_cache_behavior, "min_ttl", null)
default_ttl = lookup(var.default_cache_behavior, "default_ttl", null)
max_ttl = lookup(var.default_cache_behavior, "max_ttl", null)
viewer_protocol_policy = lookup(var.default_cache_behavior, "viewer_protocol_policy", "redirect-to-https")
field_level_encryption_id = lookup(var.default_cache_behavior, "field_level_encryption_id", null)
smooth_streaming = lookup(var.default_cache_behavior, "smooth_streaming", null)
trusted_signers = lookup(var.default_cache_behavior, "trusted_signers", null)
trusted_key_groups = lookup(var.default_cache_behavior, "trusted_key_groups", null)
cache_policy_id = lookup(var.default_cache_behavior, "cache_policy_id", null)
origin_request_policy_id = lookup(var.default_cache_behavior, "origin_request_policy_id", null)
response_headers_policy_id = lookup(var.default_cache_behavior, "response_headers_policy_id", null)
realtime_log_config_arn = lookup(var.default_cache_behavior, "realtime_log_config_arn", null)
dynamic "forwarded_values" {
for_each = lookup(var.default_cache_behavior, "use_forwarded_values", true) ? [true] : []
content {
query_string = lookup(var.default_cache_behavior, "query_string", false)
query_string_cache_keys = lookup(var.default_cache_behavior, "query_string_cache_keys", [])
headers = lookup(var.default_cache_behavior, "headers", [])
cookies {
forward = lookup(var.default_cache_behavior, "cookies_forward", "none")
whitelisted_names = lookup(var.default_cache_behavior, "cookies_whitelisted_names", null)
}
}
}
dynamic "lambda_function_association" {
for_each = lookup(var.default_cache_behavior, "lambda_function_association", [])
iterator = lambda_function
content {
event_type = lambda_function.value.event_type
lambda_arn = lambda_function.value.lambda_arn
include_body = lookup(lambda_function.value, "include_body", null)
}
}
dynamic "function_association" {
for_each = lookup(var.default_cache_behavior, "function_association", [])
iterator = function
content {
event_type = function.value.event_type
function_arn = function.value.function_arn
}
}
}
dynamic "origin" {
for_each = var.origin
content {
domain_name = origin.value.domain_name
origin_id = lookup(origin.value, "origin_id", origin.key)
origin_path = lookup(origin.value, "origin_path", "")
connection_attempts = lookup(origin.value, "connection_attempts", null)
connection_timeout = lookup(origin.value, "connection_timeout", null)
origin_access_control_id = lookup(origin.value, "origin_access_control_id", null)
dynamic "s3_origin_config" {
for_each = length(keys(lookup(origin.value, "s3_origin_config", {}))) == 0 ? [] : [lookup(origin.value, "s3_origin_config", {})]
content {
origin_access_identity = lookup(s3_origin_config.value, "cloudfront_access_identity_path",
lookup(
lookup(
aws_cloudfront_origin_access_identity.this,
lookup(s3_origin_config.value, "origin_access_identity", ""),
{}
),
"cloudfront_access_identity_path",
null
)
)
}
}
dynamic "custom_origin_config" {
for_each = length(lookup(origin.value, "custom_origin_config", "")) == 0 ? [] : [lookup(origin.value, "custom_origin_config", "")]
content {
http_port = custom_origin_config.value.http_port
https_port = custom_origin_config.value.https_port
origin_protocol_policy = custom_origin_config.value.origin_protocol_policy
origin_ssl_protocols = custom_origin_config.value.origin_ssl_protocols
origin_keepalive_timeout = lookup(custom_origin_config.value, "origin_keepalive_timeout", null)
origin_read_timeout = lookup(custom_origin_config.value, "origin_read_timeout", null)
}
}
dynamic "custom_header" {
for_each = lookup(origin.value, "custom_header", [])
content {
name = custom_header.value.name
value = custom_header.value.value
}
}
dynamic "origin_shield" {
for_each = length(keys(lookup(origin.value, "origin_shield", {}))) == 0 ? [] : [lookup(origin.value, "origin_shield", {})]
content {
enabled = origin_shield.value.enabled
origin_shield_region = origin_shield.value.origin_shield_region
}
}
}
}
dynamic "ordered_cache_behavior" {
for_each = var.ordered_cache_behaviors
iterator = cache_behavior
content {
path_pattern = cache_behavior.value["path_pattern"]
target_origin_id = cache_behavior.value["target_origin_id"]
viewer_protocol_policy = cache_behavior.value["viewer_protocol_policy"]
allowed_methods = lookup(cache_behavior.value, "allowed_methods", ["GET", "HEAD", "OPTIONS"])
cached_methods = lookup(cache_behavior.value, "cached_methods", ["GET", "HEAD"])
compress = lookup(cache_behavior.value, "compress", null)
field_level_encryption_id = lookup(cache_behavior.value, "field_level_encryption_id", null)
smooth_streaming = lookup(cache_behavior.value, "smooth_streaming", null)
trusted_signers = lookup(cache_behavior.value, "trusted_signers", null)
trusted_key_groups = lookup(cache_behavior.value, "trusted_key_groups", null)
cache_policy_id = lookup(cache_behavior.value, "cache_policy_id", null)
origin_request_policy_id = lookup(cache_behavior.value, "origin_request_policy_id", null)
response_headers_policy_id = lookup(cache_behavior.value, "response_headers_policy_id", null)
realtime_log_config_arn = lookup(cache_behavior.value, "realtime_log_config_arn", null)
min_ttl = lookup(cache_behavior.value, "min_ttl", null)
default_ttl = lookup(cache_behavior.value, "default_ttl", null)
max_ttl = lookup(cache_behavior.value, "max_ttl", null)
dynamic "forwarded_values" {
for_each = lookup(cache_behavior.value, "use_forwarded_values", true) ? [true] : []
content {
query_string = lookup(cache_behavior.value, "query_string", false)
query_string_cache_keys = lookup(cache_behavior.value, "query_string_cache_keys", [])
headers = lookup(cache_behavior.value, "headers", [])
cookies {
forward = lookup(cache_behavior.value, "cookies_forward", "none")
whitelisted_names = lookup(cache_behavior.value, "cookies_whitelisted_names", null)
}
}
}
dynamic "lambda_function_association" {
for_each = lookup(cache_behavior.value, "lambda_function_association", [])
iterator = lambda_function
content {
event_type = lambda_function.value.event_type
lambda_arn = lambda_function.value.lambda_arn
include_body = lookup(lambda_function.value, "include_body", null)
}
}
dynamic "function_association" {
for_each = lookup(cache_behavior.value, "function_association", [])
iterator = function
content {
event_type = function.value.event_type
function_arn = function.value.function_arn
}
}
}
}
dynamic "custom_error_response" {
for_each = ["400", "403", "404", "405", "414", "500", "501", "502", "503", "504"]
content {
error_code = custom_error_response.value
}
}
restrictions {
geo_restriction {
restriction_type = var.geo_restriction_config.geo_restriction_type
locations = var.geo_restriction_config.geo_restriction_locations
}
}
viewer_certificate {
acm_certificate_arn = local.is_use_cloudfront_cert_viewer ? null : var.cdn_certificate_arn
cloudfront_default_certificate = local.is_use_cloudfront_cert_viewer ? true : false
minimum_protocol_version = local.is_use_cloudfront_cert_viewer ? "TLSv1" : "TLSv1.2_2018"
ssl_support_method = local.is_use_cloudfront_cert_viewer ? null : "sni-only"
}
logging_config {
include_cookies = var.log_include_cookies
bucket = "${var.log_aggregation_s3_bucket_name}.s3.amazonaws.com"
prefix = "${var.environment}/${local.name}-cloudfront"
}
tags = merge(local.tags, { "Name" : local.name })
}
/* -------------------------------------------------------------------------- */
/* IAM */
/* -------------------------------------------------------------------------- */
resource "aws_iam_role" "main" {
count = var.is_create_log_access_role ? 1 : 0
name = "${local.name}-cloudfront-logs-access-role"
path = "/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"AWS": "*"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
tags = merge(local.tags, { "Name" : local.name })
}
resource "aws_iam_role_policy" "main" {
count = var.is_create_log_access_role ? 1 : 0
name = "${local.name}-cloudfront-logs-access-policy"
role = aws_iam_role.main[0].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:GetBucketAcl",
"s3:PutBucketAcl"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::${var.log_aggregation_s3_bucket_name}"
}
]
}
EOF
}