@@ -84,6 +84,9 @@ resource "aws_cloudfront_origin_access_identity" "this" {
84
84
# TODO
85
85
# - `default_cache_behavior.trusted_key_groups`
86
86
# - `ordered_cache_behavior.trusted_key_groups`
87
+ # - `continuous_deployment_policy_id`
88
+ # - `staging`
89
+ # - `origin.origin_access_control_id`
87
90
resource "aws_cloudfront_distribution" "this" {
88
91
aliases = var. aliases
89
92
comment = var. description
@@ -114,11 +117,11 @@ resource "aws_cloudfront_distribution" "this" {
114
117
}
115
118
116
119
117
- # # Restriction
120
+ # # Geographic Restriction
118
121
restrictions {
119
122
geo_restriction {
120
- restriction_type = lower (var. restriction_type )
121
- locations = var. restriction_locations
123
+ restriction_type = lower (var. geographic_restriction . type )
124
+ locations = var. geographic_restriction . countries
122
125
}
123
126
}
124
127
@@ -143,13 +146,13 @@ resource "aws_cloudfront_distribution" "this" {
143
146
content {
144
147
origin_id = s3. key
145
148
domain_name = s3. value . host
146
- origin_path = try ( s3. value . path , null )
149
+ origin_path = s3. value . path
147
150
148
- connection_attempts = try ( s3. value . connection_attempts , null )
149
- connection_timeout = try ( s3. value . connection_timeout , null )
151
+ connection_attempts = s3. value . connection_attempts
152
+ connection_timeout = s3. value . connection_timeout
150
153
151
154
dynamic "custom_header" {
152
- for_each = try ( s3. value . custom_headers , {})
155
+ for_each = s3. value . custom_headers
153
156
154
157
content {
155
158
name = custom_header. key
@@ -158,7 +161,7 @@ resource "aws_cloudfront_distribution" "this" {
158
161
}
159
162
160
163
dynamic "origin_shield" {
161
- for_each = try ( s3. value . origin_shield . enabled , false ) ? [s3 . value . origin_shield ] : []
164
+ for_each = s3. value . origin_shield != null ? [s3 . value . origin_shield ] : []
162
165
163
166
content {
164
167
enabled = origin_shield. value . enabled
@@ -180,13 +183,13 @@ resource "aws_cloudfront_distribution" "this" {
180
183
content {
181
184
origin_id = custom. key
182
185
domain_name = custom. value . host
183
- origin_path = try ( custom. value . path , null )
186
+ origin_path = custom. value . path
184
187
185
- connection_attempts = try ( custom. value . connection_attempts , null )
186
- connection_timeout = try ( custom. value . connection_timeout , null )
188
+ connection_attempts = custom. value . connection_attempts
189
+ connection_timeout = custom. value . connection_timeout
187
190
188
191
dynamic "custom_header" {
189
- for_each = try ( custom. value . custom_headers , {})
192
+ for_each = custom. value . custom_headers
190
193
191
194
content {
192
195
name = custom_header. key
@@ -195,7 +198,7 @@ resource "aws_cloudfront_distribution" "this" {
195
198
}
196
199
197
200
dynamic "origin_shield" {
198
- for_each = try ( custom. value . origin_shield . enabled , false ) ? [custom . value . origin_shield ] : []
201
+ for_each = custom. value . origin_shield != null ? [custom . value . origin_shield ] : []
199
202
200
203
content {
201
204
enabled = origin_shield. value . enabled
@@ -204,19 +207,13 @@ resource "aws_cloudfront_distribution" "this" {
204
207
}
205
208
206
209
custom_origin_config {
207
- http_port = try (custom. value . http_port , 80 )
208
- https_port = try (custom. value . https_port , 443 )
209
- origin_protocol_policy = try (
210
- local. origin_protocol_policy [custom . value . protocol_policy ],
211
- local. origin_protocol_policy [" MATCH_VIEWER" ]
212
- )
213
- origin_ssl_protocols = try (
214
- local. origin_ssl_security_policy [custom . value . ssl_security_policy ],
215
- local. origin_ssl_security_policy [" TLSv1.1" ]
216
- )
210
+ http_port = custom. value . http_port
211
+ https_port = custom. value . https_port
212
+ origin_protocol_policy = local. origin_protocol_policy [custom . value . protocol_policy ]
213
+ origin_ssl_protocols = local. origin_ssl_security_policy [custom . value . ssl_security_policy ]
217
214
218
- origin_keepalive_timeout = try ( custom. value . keepalive_timeout , null )
219
- origin_read_timeout = try ( custom. value . response_timeout , null )
215
+ origin_keepalive_timeout = custom. value . keepalive_timeout
216
+ origin_read_timeout = custom. value . response_timeout
220
217
}
221
218
}
222
219
}
@@ -245,31 +242,31 @@ resource "aws_cloudfront_distribution" "this" {
245
242
246
243
# # Default Behavior
247
244
default_cache_behavior {
248
- target_origin_id = var. default_target_origin
245
+ target_origin_id = var. default_behavior . target_origin
249
246
250
- compress = var. default_compression_enabled
251
- smooth_streaming = var. default_smooth_streaming_enabled
247
+ compress = var. default_behavior . compression_enabled
248
+ smooth_streaming = var. default_behavior . smooth_streaming_enabled
252
249
253
- field_level_encryption_id = (var. default_viewer_protocol_policy == " HTTPS_ONLY" && contains (var. default_allowed_http_methods , " POST" ) && contains (var. default_allowed_http_methods , " PUT" )
254
- ? var. default_field_level_encryption_configuration
250
+ field_level_encryption_id = (var. default_behavior . viewer_protocol_policy == " HTTPS_ONLY" && contains (var. default_behavior . allowed_http_methods , " POST" ) && contains (var. default_behavior . allowed_http_methods , " PUT" )
251
+ ? var. default_behavior . field_level_encryption_configuration
255
252
: null
256
253
)
257
- realtime_log_config_arn = var. default_realtime_log_configuration
254
+ realtime_log_config_arn = var. default_behavior . realtime_log_configuration
258
255
259
256
# Viewer
260
- viewer_protocol_policy = local. viewer_protocol_policy [var . default_viewer_protocol_policy ]
261
- allowed_methods = var. default_allowed_http_methods
262
- cached_methods = var. default_cached_http_methods
257
+ viewer_protocol_policy = local. viewer_protocol_policy [var . default_behavior . viewer_protocol_policy ]
258
+ allowed_methods = var. default_behavior . allowed_http_methods
259
+ cached_methods = var. default_behavior . cached_http_methods
263
260
264
261
# Policies
265
- cache_policy_id = var. default_cache_policy
266
- origin_request_policy_id = var. default_origin_request_policy
267
- response_headers_policy_id = var. default_response_headers_policy
262
+ cache_policy_id = var. default_behavior . cache_policy
263
+ origin_request_policy_id = var. default_behavior . origin_request_policy
264
+ response_headers_policy_id = var. default_behavior . response_headers_policy
268
265
269
266
# Function Associations
270
267
dynamic "lambda_function_association" {
271
268
for_each = {
272
- for event , f in try ( var. default_function_associations , {}) :
269
+ for event , f in var . default_behavior . function_associations :
273
270
event = > f
274
271
if contains (keys (local. cloudfront_events ), event) && f . type == " LAMBDA_EDGE"
275
272
}
@@ -279,12 +276,12 @@ resource "aws_cloudfront_distribution" "this" {
279
276
event_type = local. cloudfront_events [lambda . key ]
280
277
lambda_arn = lambda. value . function
281
278
282
- include_body = try ( lambda. value . include_body , false )
279
+ include_body = lambda. value . include_body
283
280
}
284
281
}
285
282
dynamic "function_association" {
286
283
for_each = {
287
- for event , f in try ( var. default_function_associations , {}) :
284
+ for event , f in var . default_behavior . function_associations :
288
285
event = > f
289
286
if contains ([" VIEWER_REQUEST" , " VIEWER_RESPONSE" ], event) && f . type == " CLOUDFRONT"
290
287
}
@@ -297,30 +294,39 @@ resource "aws_cloudfront_distribution" "this" {
297
294
}
298
295
299
296
# Cache Key & Origin Requests (Legacy)
300
- min_ttl = (var. default_cache_policy == null
301
- ? try ( var. default_cache_ttl . min , 0 )
297
+ min_ttl = (var. default_behavior . legacy_cache_config . enabled
298
+ ? var. default_behavior . legacy_cache_config . min_ttl
302
299
: null
303
300
)
304
- default_ttl = (var. default_cache_policy == null
305
- ? try ( var. default_cache_ttl . default , 0 )
301
+ default_ttl = (var. default_behavior . legacy_cache_config . enabled
302
+ ? var. default_behavior . legacy_cache_config . default_ttl
306
303
: null
307
304
)
308
- max_ttl = (var. default_cache_policy == null
309
- ? try ( var. default_cache_ttl . max , 0 )
305
+ max_ttl = (var. default_behavior . legacy_cache_config . enabled
306
+ ? var. default_behavior . legacy_cache_config . max_ttl
310
307
: null
311
308
)
312
309
313
310
dynamic "forwarded_values" {
314
- for_each = var. default_cache_policy == null ? [" go" ] : []
311
+ for_each = var. default_behavior . legacy_cache_config . enabled ? [var . default_behavior . legacy_cache_config ] : []
312
+ iterator = config
315
313
316
314
content {
317
- headers = []
318
- query_string = true
319
-
320
315
cookies {
321
- forward = " none "
322
- whitelisted_names = []
316
+ forward = lower (config . forwarding_cookies . behavior )
317
+ whitelisted_names = config . value . forwarding_cookies . items
323
318
}
319
+
320
+ headers = (config. value . forwarding_query_strings . behavior == " ALL"
321
+ ? [" *" ]
322
+ : config. value . forwarding_query_strings . items
323
+ )
324
+
325
+ query_string = contains ([" ALL" , " WHITELIST" ], config. value . forwarding_query_strings . behavior )
326
+ query_string_cache_keys = (config. value . forwarding_query_strings . behavior == " ALL"
327
+ ? null
328
+ : config. value . forwarding_query_strings . items
329
+ )
324
330
}
325
331
}
326
332
}
@@ -335,32 +341,29 @@ resource "aws_cloudfront_distribution" "this" {
335
341
path_pattern = behavior. value . path_pattern
336
342
target_origin_id = behavior. value . target_origin
337
343
338
- compress = try ( behavior. value . compression_enabled , true )
339
- smooth_streaming = try ( behavior. value . smooth_streaming_enabled , false )
344
+ compress = behavior. value . compression_enabled
345
+ smooth_streaming = behavior. value . smooth_streaming_enabled
340
346
341
- # Viewer
342
- viewer_protocol_policy = try (
343
- local. viewer_protocol_policy [behavior . value . viewer_protocol_policy ],
344
- local. viewer_protocol_policy [" REDIRECT_TO_HTTPS" ],
345
- )
346
- allowed_methods = try (
347
- toset (behavior. value . allowed_http_methods ),
348
- toset ([" GET" , " HEAD" ])
349
- )
350
- cached_methods = try (
351
- toset (behavior. value . cached_http_methods ),
352
- toset ([" GET" , " HEAD" ])
347
+ field_level_encryption_id = (behavior. value . viewer_protocol_policy == " HTTPS_ONLY" && contains (behavior. value . allowed_http_methods , " POST" ) && contains (behavior. value . allowed_http_methods , " PUT" )
348
+ ? behavior. value . field_level_encryption_configuration
349
+ : null
353
350
)
351
+ realtime_log_config_arn = behavior. value . realtime_log_configuration
352
+
353
+ # Viewer
354
+ viewer_protocol_policy = local. viewer_protocol_policy [behavior . value . viewer_protocol_policy ]
355
+ allowed_methods = behavior. value . allowed_http_methods
356
+ cached_methods = behavior. value . cached_http_methods
354
357
355
358
# Policies
356
- cache_policy_id = try ( behavior. value . cache_policy , null )
357
- origin_request_policy_id = try ( behavior. value . origin_request_policy , null )
358
- response_headers_policy_id = try ( behavior. value . response_headers_policy , null )
359
+ cache_policy_id = behavior. value . cache_policy
360
+ origin_request_policy_id = behavior. value . origin_request_policy
361
+ response_headers_policy_id = behavior. value . response_headers_policy
359
362
360
363
# Function Associations
361
364
dynamic "lambda_function_association" {
362
365
for_each = {
363
- for event , f in try ( behavior. value . function_associations , {}) :
366
+ for event , f in behavior . value . function_associations :
364
367
event = > f
365
368
if contains (keys (local. cloudfront_events ), event) && f . type == " LAMBDA_EDGE"
366
369
}
@@ -370,12 +373,12 @@ resource "aws_cloudfront_distribution" "this" {
370
373
event_type = local. cloudfront_events [lambda . key ]
371
374
lambda_arn = lambda. value . function
372
375
373
- include_body = try ( lambda. value . include_body , false )
376
+ include_body = lambda. value . include_body
374
377
}
375
378
}
376
379
dynamic "function_association" {
377
380
for_each = {
378
- for event , f in try ( behavior. value . function_associations , {}) :
381
+ for event , f in behavior . value . function_associations :
379
382
event = > f
380
383
if contains ([" VIEWER_REQUEST" , " VIEWER_RESPONSE" ], event) && f . type == " CLOUDFRONT"
381
384
}
@@ -388,30 +391,39 @@ resource "aws_cloudfront_distribution" "this" {
388
391
}
389
392
390
393
# Cache Key & Origin Requests (Legacy)
391
- min_ttl = (behavior. value . cache_policy == null
392
- ? try ( behavior. cache_ttl . min , 0 )
394
+ min_ttl = (behavior. value . legacy_cache_config . enabled
395
+ ? behavior. legacy_cache_config . min_ttl
393
396
: null
394
397
)
395
- default_ttl = (behavior. value . cache_policy == null
396
- ? try ( behavior. cache_ttl . default , 0 )
398
+ default_ttl = (behavior. value . legacy_cache_config . enabled
399
+ ? behavior. legacy_cache_config . default_ttl
397
400
: null
398
401
)
399
- max_ttl = (behavior. value . cache_policy == null
400
- ? try ( behavior. cache_ttl . max , 0 )
402
+ max_ttl = (behavior. value . legacy_cache_config . enabled
403
+ ? behavior. legacy_cache_config . max_ttl
401
404
: null
402
405
)
403
406
404
407
dynamic "forwarded_values" {
405
- for_each = behavior. value . cache_policy == null ? [" go" ] : []
408
+ for_each = behavior. value . legacy_cache_config . enabled ? [behavior . value . legacy_cache_config ] : []
409
+ iterator = config
406
410
407
411
content {
408
- headers = []
409
- query_string = true
410
-
411
412
cookies {
412
- forward = " none "
413
- whitelisted_names = []
413
+ forward = lower (config . forwarding_cookies . behavior )
414
+ whitelisted_names = config . value . forwarding_cookies . items
414
415
}
416
+
417
+ headers = (config. value . forwarding_query_strings . behavior == " ALL"
418
+ ? [" *" ]
419
+ : config. value . forwarding_query_strings . items
420
+ )
421
+
422
+ query_string = contains ([" ALL" , " WHITELIST" ], config. value . forwarding_query_strings . behavior )
423
+ query_string_cache_keys = (config. value . forwarding_query_strings . behavior == " ALL"
424
+ ? null
425
+ : config. value . forwarding_query_strings . items
426
+ )
415
427
}
416
428
}
417
429
}
0 commit comments