forked from terraform-aws-modules/terraform-aws-cloudfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
288 lines (231 loc) · 10.3 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
locals {
create_origin_access_identity = var.create_origin_access_identity && length(keys(var.origin_access_identities)) > 0
}
resource "aws_cloudfront_origin_access_identity" "this" {
for_each = local.create_origin_access_identity ? var.origin_access_identities : {}
comment = each.value
lifecycle {
create_before_destroy = true
}
}
resource "aws_cloudfront_distribution" "this" {
count = var.create_distribution ? 1 : 0
aliases = var.aliases
comment = var.comment
default_root_object = var.default_root_object
enabled = var.enabled
http_version = var.http_version
is_ipv6_enabled = var.is_ipv6_enabled
price_class = var.price_class
retain_on_delete = var.retain_on_delete
wait_for_deployment = var.wait_for_deployment
web_acl_id = var.web_acl_id
tags = var.tags
lifecycle {
# origin_path managed by external deploy script
ignore_changes = [
origin[0].origin_path,
]
}
dynamic "logging_config" {
for_each = length(keys(var.logging_config)) == 0 ? [] : [var.logging_config]
content {
bucket = logging_config.value["bucket"]
prefix = lookup(logging_config.value, "prefix", null)
include_cookies = lookup(logging_config.value, "include_cookies", null)
}
}
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)
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 "origin_group" {
for_each = var.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"]
}
}
}
dynamic "default_cache_behavior" {
for_each = [var.default_cache_behavior]
iterator = i
content {
target_origin_id = i.value["target_origin_id"]
viewer_protocol_policy = i.value["viewer_protocol_policy"]
allowed_methods = lookup(i.value, "allowed_methods", ["GET", "HEAD", "OPTIONS"])
cached_methods = lookup(i.value, "cached_methods", ["GET", "HEAD"])
compress = lookup(i.value, "compress", null)
field_level_encryption_id = lookup(i.value, "field_level_encryption_id", null)
smooth_streaming = lookup(i.value, "smooth_streaming", null)
trusted_signers = lookup(i.value, "trusted_signers", null)
trusted_key_groups = lookup(i.value, "trusted_key_groups", null)
cache_policy_id = lookup(i.value, "cache_policy_id", null)
origin_request_policy_id = lookup(i.value, "origin_request_policy_id", null)
response_headers_policy_id = lookup(i.value, "response_headers_policy_id", null)
realtime_log_config_arn = lookup(i.value, "realtime_log_config_arn", null)
min_ttl = lookup(i.value, "min_ttl", null)
default_ttl = lookup(i.value, "default_ttl", null)
max_ttl = lookup(i.value, "max_ttl", null)
dynamic "forwarded_values" {
for_each = lookup(i.value, "use_forwarded_values", true) ? [true] : []
content {
query_string = lookup(i.value, "query_string", false)
query_string_cache_keys = lookup(i.value, "query_string_cache_keys", [])
headers = lookup(i.value, "headers", [])
cookies {
forward = lookup(i.value, "cookies_forward", "none")
whitelisted_names = lookup(i.value, "cookies_whitelisted_names", null)
}
}
}
dynamic "lambda_function_association" {
for_each = lookup(i.value, "lambda_function_association", [])
iterator = l
content {
event_type = l.key
lambda_arn = l.value.lambda_arn
include_body = lookup(l.value, "include_body", null)
}
}
dynamic "function_association" {
for_each = lookup(i.value, "function_association", [])
iterator = f
content {
event_type = f.key
function_arn = f.value.function_arn
}
}
}
}
dynamic "ordered_cache_behavior" {
for_each = var.ordered_cache_behavior
iterator = i
content {
path_pattern = i.value["path_pattern"]
target_origin_id = i.value["target_origin_id"]
viewer_protocol_policy = i.value["viewer_protocol_policy"]
allowed_methods = lookup(i.value, "allowed_methods", ["GET", "HEAD", "OPTIONS"])
cached_methods = lookup(i.value, "cached_methods", ["GET", "HEAD"])
compress = lookup(i.value, "compress", null)
field_level_encryption_id = lookup(i.value, "field_level_encryption_id", null)
smooth_streaming = lookup(i.value, "smooth_streaming", null)
trusted_signers = lookup(i.value, "trusted_signers", null)
trusted_key_groups = lookup(i.value, "trusted_key_groups", null)
cache_policy_id = lookup(i.value, "cache_policy_id", null)
origin_request_policy_id = lookup(i.value, "origin_request_policy_id", null)
response_headers_policy_id = lookup(i.value, "response_headers_policy_id", null)
realtime_log_config_arn = lookup(i.value, "realtime_log_config_arn", null)
min_ttl = lookup(i.value, "min_ttl", null)
default_ttl = lookup(i.value, "default_ttl", null)
max_ttl = lookup(i.value, "max_ttl", null)
dynamic "forwarded_values" {
for_each = lookup(i.value, "use_forwarded_values", true) ? [true] : []
content {
query_string = lookup(i.value, "query_string", false)
query_string_cache_keys = lookup(i.value, "query_string_cache_keys", [])
headers = lookup(i.value, "headers", [])
cookies {
forward = lookup(i.value, "cookies_forward", "none")
whitelisted_names = lookup(i.value, "cookies_whitelisted_names", null)
}
}
}
dynamic "lambda_function_association" {
for_each = lookup(i.value, "lambda_function_association", [])
iterator = l
content {
event_type = l.key
lambda_arn = l.value.lambda_arn
include_body = lookup(l.value, "include_body", null)
}
}
dynamic "function_association" {
for_each = lookup(i.value, "function_association", [])
iterator = f
content {
event_type = f.key
function_arn = f.value.function_arn
}
}
}
}
viewer_certificate {
acm_certificate_arn = lookup(var.viewer_certificate, "acm_certificate_arn", null)
cloudfront_default_certificate = lookup(var.viewer_certificate, "cloudfront_default_certificate", null)
iam_certificate_id = lookup(var.viewer_certificate, "iam_certificate_id", null)
minimum_protocol_version = lookup(var.viewer_certificate, "minimum_protocol_version", "TLSv1")
ssl_support_method = lookup(var.viewer_certificate, "ssl_support_method", null)
}
dynamic "custom_error_response" {
for_each = var.custom_error_response
content {
error_code = custom_error_response.value["error_code"]
response_code = lookup(custom_error_response.value, "response_code", null)
response_page_path = lookup(custom_error_response.value, "response_page_path", null)
error_caching_min_ttl = lookup(custom_error_response.value, "error_caching_min_ttl", null)
}
}
restrictions {
dynamic "geo_restriction" {
for_each = [var.geo_restriction]
content {
restriction_type = lookup(geo_restriction.value, "restriction_type", "none")
locations = lookup(geo_restriction.value, "locations", [])
}
}
}
}
resource "aws_cloudfront_monitoring_subscription" "this" {
count = var.create_distribution && var.create_monitoring_subscription ? 1 : 0
distribution_id = aws_cloudfront_distribution.this[0].id
monitoring_subscription {
realtime_metrics_subscription_config {
realtime_metrics_subscription_status = var.realtime_metrics_subscription_status
}
}
}