Skip to content

Commit 041f17f

Browse files
committed
feat: complex type for ordered_cache_behavior
1 parent db81072 commit 041f17f

File tree

3 files changed

+134
-63
lines changed

3 files changed

+134
-63
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ No modules.
145145
| <a name="input_create_origin_access_identity"></a> [create\_origin\_access\_identity](#input\_create\_origin\_access\_identity) | Controls if CloudFront origin access identity should be created | `bool` | `false` | no |
146146
| <a name="input_create_vpc_origin"></a> [create\_vpc\_origin](#input\_create\_vpc\_origin) | If enabled, the resource for VPC origin will be created. | `bool` | `false` | no |
147147
| <a name="input_custom_error_response"></a> [custom\_error\_response](#input\_custom\_error\_response) | One or more custom error response elements | `any` | `{}` | no |
148-
| <a name="input_default_cache_behavior"></a> [default\_cache\_behavior](#input\_default\_cache\_behavior) | The default cache behavior for this distribution | <pre>object({<br/> allowed_methods = list(string)<br/> cached_methods = list(string)<br/> cache_policy_id = optional(string)<br/> compress = optional(bool)<br/> default_ttl = optional(number)<br/> field_level_encryption_id = optional(string)<br/> forwarded_values = optional(object({<br/> cookies = object({<br/> forward = string<br/> whitelisted_names = optional(list(string))<br/> })<br/> headers = optional(list(string))<br/> query_string = bool<br/> query_string_cache_keys = optional(list(string))<br/> }))<br/> lambda_function_association = optional(map(object({<br/> # event_type = map key<br/> lambda_arn = string<br/> include_body = optional(bool)<br/> })), {})<br/> function_association = optional(map(object({<br/> # event_type = map key<br/> function_arn = string<br/> })), {})<br/> max_ttl = optional(number)<br/> min_ttl = optional(number)<br/> origin_request_policy_id = optional(string)<br/> realtime_log_config_arn = optional(string)<br/> response_headers_policy_id = optional(string)<br/> smooth_streaming = optional(bool)<br/> target_origin_id = string<br/> trusted_key_groups = optional(list(string))<br/> trusted_signers = optional(list(string))<br/> viewer_protocol_policy = string<br/> grpc_config = optional(object({<br/> enabled = bool<br/> }))<br/> })</pre> | n/a | yes |
148+
| <a name="input_default_cache_behavior"></a> [default\_cache\_behavior](#input\_default\_cache\_behavior) | The default cache behavior for this distribution | <pre>object({<br/> allowed_methods = list(string)<br/> cached_methods = list(string)<br/> cache_policy_id = optional(string)<br/> cache_policy_name = optional(string) # convenience variable to lookup<br/> compress = optional(bool)<br/> default_ttl = optional(number)<br/> field_level_encryption_id = optional(string)<br/> forwarded_values = optional(object({<br/> cookies = object({<br/> forward = string<br/> whitelisted_names = optional(list(string))<br/> })<br/> headers = optional(list(string))<br/> query_string = bool<br/> query_string_cache_keys = optional(list(string))<br/> }))<br/> lambda_function_association = optional(map(object({<br/> # event_type = map key<br/> lambda_arn = string<br/> include_body = optional(bool)<br/> })), {})<br/> function_association = optional(map(object({<br/> # event_type = map key<br/> function_arn = string<br/> })), {})<br/> max_ttl = optional(number)<br/> min_ttl = optional(number)<br/> origin_request_policy_id = optional(string)<br/> origin_request_policy_name = optional(string) # convenience variable to lookup<br/> realtime_log_config_arn = optional(string)<br/> response_headers_policy_id = optional(string)<br/> response_headers_policy_name = optional(string) # convenience variable to lookup<br/> smooth_streaming = optional(bool)<br/> target_origin_id = string<br/> trusted_key_groups = optional(list(string))<br/> trusted_signers = optional(list(string))<br/> viewer_protocol_policy = string<br/> grpc_config = optional(object({<br/> enabled = bool<br/> }))<br/> })</pre> | n/a | yes |
149149
| <a name="input_default_root_object"></a> [default\_root\_object](#input\_default\_root\_object) | The object that you want CloudFront to return (for example, index.html) when an end user requests the root URL. | `string` | `null` | no |
150150
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Whether the distribution is enabled to accept end user requests for content. | `bool` | `true` | no |
151151
| <a name="input_geo_restriction"></a> [geo\_restriction](#input\_geo\_restriction) | The restriction configuration for this distribution (geo\_restrictions) | `any` | `{}` | no |
152152
| <a name="input_http_version"></a> [http\_version](#input\_http\_version) | The maximum HTTP version to support on the distribution. Allowed values are http1.1, http2, http2and3, and http3. The default is http2. | `string` | `"http2"` | no |
153153
| <a name="input_is_ipv6_enabled"></a> [is\_ipv6\_enabled](#input\_is\_ipv6\_enabled) | Whether the IPv6 is enabled for the distribution. | `bool` | `null` | no |
154154
| <a name="input_logging_config"></a> [logging\_config](#input\_logging\_config) | The logging configuration that controls how logs are written to your distribution (maximum one). | `any` | `{}` | no |
155-
| <a name="input_ordered_cache_behavior"></a> [ordered\_cache\_behavior](#input\_ordered\_cache\_behavior) | An ordered list of cache behaviors resource for this distribution. List from top to bottom in order of precedence. The topmost cache behavior will have precedence 0. | `any` | `[]` | no |
155+
| <a name="input_ordered_cache_behavior"></a> [ordered\_cache\_behavior](#input\_ordered\_cache\_behavior) | An ordered list of cache behaviors resource for this distribution. List from top to bottom in order of precedence. The topmost cache behavior will have precedence 0. | <pre>list(object({<br/> allowed_methods = list(string)<br/> cached_methods = list(string)<br/> cache_policy_id = optional(string)<br/> cache_policy_name = optional(string) # convenience variable to lookup<br/> compress = optional(bool)<br/> default_ttl = optional(number)<br/> field_level_encryption_id = optional(string)<br/> forwarded_values = optional(object({<br/> cookies = object({<br/> forward = string<br/> whitelisted_names = optional(list(string))<br/> })<br/> headers = optional(list(string))<br/> query_string = bool<br/> query_string_cache_keys = optional(list(string))<br/> }))<br/> lambda_function_association = optional(map(object({<br/> # event_type = map key<br/> lambda_arn = string<br/> include_body = optional(bool)<br/> })), {})<br/> function_association = optional(map(object({<br/> # event_type = map key<br/> function_arn = string<br/> })), {})<br/> max_ttl = optional(number)<br/> min_ttl = optional(number)<br/> origin_request_policy_id = optional(string)<br/> origin_request_policy_name = optional(string) # convenience variable to lookup<br/> path_pattern = string<br/> realtime_log_config_arn = optional(string)<br/> response_headers_policy_id = optional(string)<br/> response_headers_policy_name = optional(string) # convenience variable to lookup<br/> smooth_streaming = optional(bool)<br/> target_origin_id = string<br/> trusted_key_groups = optional(list(string))<br/> trusted_signers = optional(list(string))<br/> viewer_protocol_policy = string<br/> grpc_config = optional(object({<br/> enabled = bool<br/> }))<br/> }))</pre> | `[]` | no |
156156
| <a name="input_origin"></a> [origin](#input\_origin) | One or more origins for this distribution (multiples allowed). | <pre>map(object({<br/> connection_attempts = optional(number)<br/> connection_timeout = optional(number)<br/> custom_origin_config = optional(object({<br/> http_port = number<br/> https_port = number<br/> origin_protocol_policy = string<br/> origin_ssl_protocols = list(string)<br/> origin_keepalive_timeout = optional(number)<br/> origin_read_timeout = optional(number)<br/> }))<br/> domain_name = string<br/> custom_header = optional(list(object({<br/> name = string<br/> value = string<br/> })), [])<br/> origin_access_control_id = optional(string)<br/> origin_id = optional(string) # If not provided, map key is used.<br/> origin_path = optional(string)<br/> origin_shield = optional(object({<br/> enabled = bool<br/> origin_shield_region = optional(string)<br/> }))<br/> s3_origin_config = optional(object({<br/> origin_access_identity = string<br/> }))<br/> vpc_origin_config = optional(object({<br/> origin_keepalive_timeout = optional(number)<br/> origin_read_timeout = optional(number)<br/> vpc_origin_id = optional(string) # If not provided, uses aws_cloudfront_vpc_origin.this[this.vpc_origin].id<br/> vpc_origin = optional(string) # Custom parameter to lookup against aws_cloudfront_vpc_origin.this<br/> }))<br/> }))</pre> | `{}` | no |
157157
| <a name="input_origin_access_control"></a> [origin\_access\_control](#input\_origin\_access\_control) | Map of CloudFront origin access control | <pre>map(object({<br/> name = optional(string)<br/> description = string<br/> origin_type = string<br/> signing_behavior = string<br/> signing_protocol = string<br/> }))</pre> | <pre>{<br/> "s3": {<br/> "description": "",<br/> "origin_type": "s3",<br/> "signing_behavior": "always",<br/> "signing_protocol": "sigv4"<br/> }<br/>}</pre> | no |
158158
| <a name="input_origin_access_identities"></a> [origin\_access\_identities](#input\_origin\_access\_identities) | Map of CloudFront origin access identities (value as a comment) | `map(string)` | `{}` | no |

main.tf

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,12 @@ resource "aws_cloudfront_distribution" "this" {
163163
}
164164

165165
default_cache_behavior {
166-
allowed_methods = var.default_cache_behavior.allowed_methods
167-
cached_methods = var.default_cache_behavior.cached_methods
168-
cache_policy_id = var.default_cache_behavior.cache_policy_id
166+
allowed_methods = var.default_cache_behavior.allowed_methods
167+
cached_methods = var.default_cache_behavior.cached_methods
168+
cache_policy_id = try(
169+
data.aws_cloudfront_cache_policy.this[var.default_cache_behavior.cache_policy_name].id,
170+
var.default_cache_behavior.cache_policy_id
171+
)
169172
compress = var.default_cache_behavior.compress
170173
default_ttl = var.default_cache_behavior.default_ttl
171174
field_level_encryption_id = var.default_cache_behavior.field_level_encryption_id
@@ -203,16 +206,22 @@ resource "aws_cloudfront_distribution" "this" {
203206
}
204207
}
205208

206-
max_ttl = var.default_cache_behavior.max_ttl
207-
min_ttl = var.default_cache_behavior.min_ttl
208-
origin_request_policy_id = var.default_cache_behavior.origin_request_policy_id
209-
realtime_log_config_arn = var.default_cache_behavior.realtime_log_config_arn
210-
response_headers_policy_id = var.default_cache_behavior.response_headers_policy_id
211-
smooth_streaming = var.default_cache_behavior.smooth_streaming
212-
target_origin_id = var.default_cache_behavior.target_origin_id
213-
trusted_key_groups = var.default_cache_behavior.trusted_key_groups
214-
trusted_signers = var.default_cache_behavior.trusted_signers
215-
viewer_protocol_policy = var.default_cache_behavior.viewer_protocol_policy
209+
max_ttl = var.default_cache_behavior.max_ttl
210+
min_ttl = var.default_cache_behavior.min_ttl
211+
origin_request_policy_id = try(
212+
data.aws_cloudfront_origin_request_policy.this[var.default_cache_behavior.origin_request_policy_name].id,
213+
var.default_cache_behavior.origin_request_policy_id
214+
)
215+
realtime_log_config_arn = var.default_cache_behavior.realtime_log_config_arn
216+
response_headers_policy_id = try(
217+
data.aws_cloudfront_response_headers_policy.this[var.default_cache_behavior.response_headers_policy_name].id,
218+
var.default_cache_behavior.response_headers_policy_id
219+
)
220+
smooth_streaming = var.default_cache_behavior.smooth_streaming
221+
target_origin_id = var.default_cache_behavior.target_origin_id
222+
trusted_key_groups = var.default_cache_behavior.trusted_key_groups
223+
trusted_signers = var.default_cache_behavior.trusted_signers
224+
viewer_protocol_policy = var.default_cache_behavior.viewer_protocol_policy
216225

217226
dynamic "grpc_config" {
218227
for_each = var.default_cache_behavior.grpc_config != null ? [var.default_cache_behavior.grpc_config] : []
@@ -229,56 +238,43 @@ resource "aws_cloudfront_distribution" "this" {
229238
iterator = i
230239

231240
content {
232-
path_pattern = i.value["path_pattern"]
233-
target_origin_id = i.value["target_origin_id"]
234-
viewer_protocol_policy = i.value["viewer_protocol_policy"]
235-
236-
allowed_methods = lookup(i.value, "allowed_methods", ["GET", "HEAD", "OPTIONS"])
237-
cached_methods = lookup(i.value, "cached_methods", ["GET", "HEAD"])
238-
compress = lookup(i.value, "compress", null)
239-
field_level_encryption_id = lookup(i.value, "field_level_encryption_id", null)
240-
smooth_streaming = lookup(i.value, "smooth_streaming", null)
241-
trusted_signers = lookup(i.value, "trusted_signers", null)
242-
trusted_key_groups = lookup(i.value, "trusted_key_groups", null)
243-
244-
cache_policy_id = try(i.value.cache_policy_id, data.aws_cloudfront_cache_policy.this[i.value.cache_policy_name].id, null)
245-
origin_request_policy_id = try(i.value.origin_request_policy_id, data.aws_cloudfront_origin_request_policy.this[i.value.origin_request_policy_name].id, null)
246-
response_headers_policy_id = try(i.value.response_headers_policy_id, data.aws_cloudfront_response_headers_policy.this[i.value.response_headers_policy_name].id, null)
247-
248-
realtime_log_config_arn = lookup(i.value, "realtime_log_config_arn", null)
249-
250-
min_ttl = lookup(i.value, "min_ttl", null)
251-
default_ttl = lookup(i.value, "default_ttl", null)
252-
max_ttl = lookup(i.value, "max_ttl", null)
241+
allowed_methods = i.value.allowed_methods
242+
cached_methods = i.value.cached_methods
243+
cache_policy_id = try(
244+
data.aws_cloudfront_cache_policy.this[i.value.cache_policy_name].id,
245+
i.value.cache_policy_id
246+
)
247+
compress = i.value.compress
248+
default_ttl = i.value.default_ttl
249+
field_level_encryption_id = i.value.field_level_encryption_id
253250

254251
dynamic "forwarded_values" {
255-
for_each = lookup(i.value, "use_forwarded_values", true) ? [true] : []
252+
for_each = i.value.forwarded_values != null ? [i.value.forwarded_values] : []
256253

257254
content {
258-
query_string = lookup(i.value, "query_string", false)
259-
query_string_cache_keys = lookup(i.value, "query_string_cache_keys", [])
260-
headers = lookup(i.value, "headers", [])
261-
262255
cookies {
263-
forward = lookup(i.value, "cookies_forward", "none")
264-
whitelisted_names = lookup(i.value, "cookies_whitelisted_names", null)
256+
forward = i.value.cookies_forward
257+
whitelisted_names = i.value.cookies_whitelisted_names
265258
}
259+
headers = i.value.headers
260+
query_string = i.value.query_string
261+
query_string_cache_keys = i.value.query_string_cache_keys
266262
}
267263
}
268264

269265
dynamic "lambda_function_association" {
270-
for_each = lookup(i.value, "lambda_function_association", [])
266+
for_each = i.value.lambda_function_association
271267
iterator = l
272268

273269
content {
274270
event_type = l.key
275271
lambda_arn = l.value.lambda_arn
276-
include_body = lookup(l.value, "include_body", null)
272+
include_body = l.value.include_body
277273
}
278274
}
279275

280276
dynamic "function_association" {
281-
for_each = lookup(i.value, "function_association", [])
277+
for_each = i.value.function_association
282278
iterator = f
283279

284280
content {
@@ -287,8 +283,27 @@ resource "aws_cloudfront_distribution" "this" {
287283
}
288284
}
289285

286+
max_ttl = i.value.max_ttl
287+
min_ttl = i.value.min_ttl
288+
origin_request_policy_id = try(
289+
data.aws_cloudfront_origin_request_policy.this[i.value.origin_request_policy_name].id,
290+
i.value.origin_request_policy_id
291+
)
292+
path_pattern = i.value.path_pattern
293+
realtime_log_config_arn = i.value.realtime_log_config_arn
294+
response_headers_policy_id = try(
295+
data.aws_cloudfront_response_headers_policy.this[i.value.response_headers_policy_name].id,
296+
i.value.response_headers_policy_id
297+
)
298+
smooth_streaming = i.value.smooth_streaming
299+
target_origin_id = i.value.target_origin_id
300+
trusted_key_groups = i.value.trusted_key_groups
301+
trusted_signers = i.value.trusted_signers
302+
viewer_protocol_policy = i.value.viewer_protocol_policy
303+
290304
dynamic "grpc_config" {
291-
for_each = try([i.value.grpc_config], [])
305+
for_each = i.value.grpc_config != null ? [i.value.grpc_config] : []
306+
292307
content {
293308
enabled = grpc_config.value.enabled
294309
}
@@ -306,7 +321,9 @@ resource "aws_cloudfront_distribution" "this" {
306321
}
307322

308323
dynamic "custom_error_response" {
309-
for_each = length(flatten([var.custom_error_response])[0]) > 0 ? flatten([var.custom_error_response]) : []
324+
for_each = length(
325+
flatten([var.custom_error_response])[0]) > 0 ? flatten([var.custom_error_response]
326+
) : []
310327

311328
content {
312329
error_code = custom_error_response.value["error_code"]
@@ -342,19 +359,28 @@ resource "aws_cloudfront_monitoring_subscription" "this" {
342359
}
343360

344361
data "aws_cloudfront_cache_policy" "this" {
345-
for_each = toset([for v in concat([var.default_cache_behavior], var.ordered_cache_behavior) : v.cache_policy_name if can(v.cache_policy_name)])
362+
for_each = toset([
363+
for v in concat([var.default_cache_behavior], var.ordered_cache_behavior) :
364+
v.cache_policy_name if can(v.cache_policy_name)
365+
])
346366

347367
name = each.key
348368
}
349369

350370
data "aws_cloudfront_origin_request_policy" "this" {
351-
for_each = toset([for v in concat([var.default_cache_behavior], var.ordered_cache_behavior) : v.origin_request_policy_name if can(v.origin_request_policy_name)])
371+
for_each = toset([
372+
for v in concat([var.default_cache_behavior], var.ordered_cache_behavior) :
373+
v.origin_request_policy_name if can(v.origin_request_policy_name)
374+
])
352375

353376
name = each.key
354377
}
355378

356379
data "aws_cloudfront_response_headers_policy" "this" {
357-
for_each = toset([for v in concat([var.default_cache_behavior], var.ordered_cache_behavior) : v.response_headers_policy_name if can(v.response_headers_policy_name)])
380+
for_each = toset([
381+
for v in concat([var.default_cache_behavior], var.ordered_cache_behavior) :
382+
v.response_headers_policy_name if can(v.response_headers_policy_name)
383+
])
358384

359385
name = each.key
360386
}

0 commit comments

Comments
 (0)