Skip to content

Commit 50dead6

Browse files
authored
Define explictly any type variables to object in distribution (#25)
1 parent 8977173 commit 50dead6

File tree

6 files changed

+375
-295
lines changed

6 files changed

+375
-295
lines changed

examples/cloudfront-distribution-simple/main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ module "distribution" {
2020
host = "api.example.com"
2121
}
2222
}
23-
default_target_origin = "api"
23+
default_behavior = {
24+
target_origin = "api"
25+
}
2426

2527
tags = {
26-
"project" = "terraform-aws-secret-examples"
28+
"project" = "terraform-aws-cloudfront-examples"
2729
}
2830
}

modules/distribution/README.md

Lines changed: 10 additions & 23 deletions
Large diffs are not rendered by default.

modules/distribution/main.tf

Lines changed: 96 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ resource "aws_cloudfront_origin_access_identity" "this" {
8484
# TODO
8585
# - `default_cache_behavior.trusted_key_groups`
8686
# - `ordered_cache_behavior.trusted_key_groups`
87+
# - `continuous_deployment_policy_id`
88+
# - `staging`
89+
# - `origin.origin_access_control_id`
8790
resource "aws_cloudfront_distribution" "this" {
8891
aliases = var.aliases
8992
comment = var.description
@@ -114,11 +117,11 @@ resource "aws_cloudfront_distribution" "this" {
114117
}
115118

116119

117-
## Restriction
120+
## Geographic Restriction
118121
restrictions {
119122
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
122125
}
123126
}
124127

@@ -143,13 +146,13 @@ resource "aws_cloudfront_distribution" "this" {
143146
content {
144147
origin_id = s3.key
145148
domain_name = s3.value.host
146-
origin_path = try(s3.value.path, null)
149+
origin_path = s3.value.path
147150

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
150153

151154
dynamic "custom_header" {
152-
for_each = try(s3.value.custom_headers, {})
155+
for_each = s3.value.custom_headers
153156

154157
content {
155158
name = custom_header.key
@@ -158,7 +161,7 @@ resource "aws_cloudfront_distribution" "this" {
158161
}
159162

160163
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] : []
162165

163166
content {
164167
enabled = origin_shield.value.enabled
@@ -180,13 +183,13 @@ resource "aws_cloudfront_distribution" "this" {
180183
content {
181184
origin_id = custom.key
182185
domain_name = custom.value.host
183-
origin_path = try(custom.value.path, null)
186+
origin_path = custom.value.path
184187

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
187190

188191
dynamic "custom_header" {
189-
for_each = try(custom.value.custom_headers, {})
192+
for_each = custom.value.custom_headers
190193

191194
content {
192195
name = custom_header.key
@@ -195,7 +198,7 @@ resource "aws_cloudfront_distribution" "this" {
195198
}
196199

197200
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] : []
199202

200203
content {
201204
enabled = origin_shield.value.enabled
@@ -204,19 +207,13 @@ resource "aws_cloudfront_distribution" "this" {
204207
}
205208

206209
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]
217214

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
220217
}
221218
}
222219
}
@@ -245,31 +242,31 @@ resource "aws_cloudfront_distribution" "this" {
245242

246243
## Default Behavior
247244
default_cache_behavior {
248-
target_origin_id = var.default_target_origin
245+
target_origin_id = var.default_behavior.target_origin
249246

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
252249

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
255252
: null
256253
)
257-
realtime_log_config_arn = var.default_realtime_log_configuration
254+
realtime_log_config_arn = var.default_behavior.realtime_log_configuration
258255

259256
# 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
263260

264261
# 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
268265

269266
# Function Associations
270267
dynamic "lambda_function_association" {
271268
for_each = {
272-
for event, f in try(var.default_function_associations, {}) :
269+
for event, f in var.default_behavior.function_associations :
273270
event => f
274271
if contains(keys(local.cloudfront_events), event) && f.type == "LAMBDA_EDGE"
275272
}
@@ -279,12 +276,12 @@ resource "aws_cloudfront_distribution" "this" {
279276
event_type = local.cloudfront_events[lambda.key]
280277
lambda_arn = lambda.value.function
281278

282-
include_body = try(lambda.value.include_body, false)
279+
include_body = lambda.value.include_body
283280
}
284281
}
285282
dynamic "function_association" {
286283
for_each = {
287-
for event, f in try(var.default_function_associations, {}) :
284+
for event, f in var.default_behavior.function_associations :
288285
event => f
289286
if contains(["VIEWER_REQUEST", "VIEWER_RESPONSE"], event) && f.type == "CLOUDFRONT"
290287
}
@@ -297,30 +294,39 @@ resource "aws_cloudfront_distribution" "this" {
297294
}
298295

299296
# 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
302299
: null
303300
)
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
306303
: null
307304
)
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
310307
: null
311308
)
312309

313310
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
315313

316314
content {
317-
headers = []
318-
query_string = true
319-
320315
cookies {
321-
forward = "none"
322-
whitelisted_names = []
316+
forward = lower(config.forwarding_cookies.behavior)
317+
whitelisted_names = config.value.forwarding_cookies.items
323318
}
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+
)
324330
}
325331
}
326332
}
@@ -335,32 +341,29 @@ resource "aws_cloudfront_distribution" "this" {
335341
path_pattern = behavior.value.path_pattern
336342
target_origin_id = behavior.value.target_origin
337343

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
340346

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
353350
)
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
354357

355358
# 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
359362

360363
# Function Associations
361364
dynamic "lambda_function_association" {
362365
for_each = {
363-
for event, f in try(behavior.value.function_associations, {}) :
366+
for event, f in behavior.value.function_associations :
364367
event => f
365368
if contains(keys(local.cloudfront_events), event) && f.type == "LAMBDA_EDGE"
366369
}
@@ -370,12 +373,12 @@ resource "aws_cloudfront_distribution" "this" {
370373
event_type = local.cloudfront_events[lambda.key]
371374
lambda_arn = lambda.value.function
372375

373-
include_body = try(lambda.value.include_body, false)
376+
include_body = lambda.value.include_body
374377
}
375378
}
376379
dynamic "function_association" {
377380
for_each = {
378-
for event, f in try(behavior.value.function_associations, {}) :
381+
for event, f in behavior.value.function_associations :
379382
event => f
380383
if contains(["VIEWER_REQUEST", "VIEWER_RESPONSE"], event) && f.type == "CLOUDFRONT"
381384
}
@@ -388,30 +391,39 @@ resource "aws_cloudfront_distribution" "this" {
388391
}
389392

390393
# 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
393396
: null
394397
)
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
397400
: null
398401
)
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
401404
: null
402405
)
403406

404407
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
406410

407411
content {
408-
headers = []
409-
query_string = true
410-
411412
cookies {
412-
forward = "none"
413-
whitelisted_names = []
413+
forward = lower(config.forwarding_cookies.behavior)
414+
whitelisted_names = config.value.forwarding_cookies.items
414415
}
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+
)
415427
}
416428
}
417429
}

0 commit comments

Comments
 (0)