Skip to content

Commit db81072

Browse files
committed
feat: complex variable type for default cache behavior
1 parent ab10f58 commit db81072

File tree

4 files changed

+88
-60
lines changed

4 files changed

+88
-60
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ 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 | `any` | `null` | 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 |
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 |

main.tf

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -162,77 +162,68 @@ resource "aws_cloudfront_distribution" "this" {
162162
}
163163
}
164164

165-
dynamic "default_cache_behavior" {
166-
for_each = [var.default_cache_behavior]
167-
iterator = i
168-
169-
content {
170-
target_origin_id = i.value["target_origin_id"]
171-
viewer_protocol_policy = i.value["viewer_protocol_policy"]
172-
173-
allowed_methods = lookup(i.value, "allowed_methods", ["GET", "HEAD", "OPTIONS"])
174-
cached_methods = lookup(i.value, "cached_methods", ["GET", "HEAD"])
175-
compress = lookup(i.value, "compress", null)
176-
field_level_encryption_id = lookup(i.value, "field_level_encryption_id", null)
177-
smooth_streaming = lookup(i.value, "smooth_streaming", null)
178-
trusted_signers = lookup(i.value, "trusted_signers", null)
179-
trusted_key_groups = lookup(i.value, "trusted_key_groups", null)
180-
181-
cache_policy_id = try(i.value.cache_policy_id, data.aws_cloudfront_cache_policy.this[i.value.cache_policy_name].id, null)
182-
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)
183-
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)
184-
185-
realtime_log_config_arn = lookup(i.value, "realtime_log_config_arn", null)
186-
187-
min_ttl = lookup(i.value, "min_ttl", null)
188-
default_ttl = lookup(i.value, "default_ttl", null)
189-
max_ttl = lookup(i.value, "max_ttl", null)
190-
191-
dynamic "forwarded_values" {
192-
for_each = lookup(i.value, "use_forwarded_values", true) ? [true] : []
165+
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
169+
compress = var.default_cache_behavior.compress
170+
default_ttl = var.default_cache_behavior.default_ttl
171+
field_level_encryption_id = var.default_cache_behavior.field_level_encryption_id
193172

194-
content {
195-
query_string = lookup(i.value, "query_string", false)
196-
query_string_cache_keys = lookup(i.value, "query_string_cache_keys", [])
197-
headers = lookup(i.value, "headers", [])
173+
dynamic "forwarded_values" {
174+
for_each = var.default_cache_behavior.forwarded_values != null ? [var.default_cache_behavior.forwarded_values] : []
198175

199-
cookies {
200-
forward = lookup(i.value, "cookies_forward", "none")
201-
whitelisted_names = lookup(i.value, "cookies_whitelisted_names", null)
202-
}
176+
content {
177+
cookies {
178+
forward = forwarded_values.value.cookies.forward
179+
whitelisted_names = forwarded_values.value.cookies.whitelisted_names
203180
}
181+
headers = forwarded_values.value.headers
182+
query_string = forwarded_values.value.query_string
183+
query_string_cache_keys = forwarded_values.value.query_string_cache_keys
204184
}
185+
}
205186

206-
dynamic "lambda_function_association" {
207-
for_each = lookup(i.value, "lambda_function_association", [])
208-
iterator = l
187+
dynamic "lambda_function_association" {
188+
for_each = var.default_cache_behavior.lambda_function_association
209189

210-
content {
211-
event_type = l.key
212-
lambda_arn = l.value.lambda_arn
213-
include_body = lookup(l.value, "include_body", null)
214-
}
190+
content {
191+
event_type = lambda_function_association.key
192+
lambda_arn = lambda_function_association.value.lambda_arn
193+
include_body = lambda_function_association.value.include_body
215194
}
195+
}
216196

217-
dynamic "function_association" {
218-
for_each = lookup(i.value, "function_association", [])
219-
iterator = f
197+
dynamic "function_association" {
198+
for_each = var.default_cache_behavior.function_association
220199

221-
content {
222-
event_type = f.key
223-
function_arn = f.value.function_arn
224-
}
200+
content {
201+
event_type = function_association.key
202+
function_arn = function_association.value.function_arn
225203
}
204+
}
226205

227-
dynamic "grpc_config" {
228-
for_each = try([i.value.grpc_config], [])
229-
content {
230-
enabled = grpc_config.value.enabled
231-
}
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
216+
217+
dynamic "grpc_config" {
218+
for_each = var.default_cache_behavior.grpc_config != null ? [var.default_cache_behavior.grpc_config] : []
219+
220+
content {
221+
enabled = grpc_config.value.enabled
232222
}
233223
}
234224
}
235225

226+
236227
dynamic "ordered_cache_behavior" {
237228
for_each = var.ordered_cache_behavior
238229
iterator = i

variables.tf

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,45 @@ variable "custom_error_response" {
193193

194194
variable "default_cache_behavior" {
195195
description = "The default cache behavior for this distribution"
196-
type = any
197-
default = null
196+
type = object({
197+
allowed_methods = list(string)
198+
cached_methods = list(string)
199+
cache_policy_id = optional(string)
200+
compress = optional(bool)
201+
default_ttl = optional(number)
202+
field_level_encryption_id = optional(string)
203+
forwarded_values = optional(object({
204+
cookies = object({
205+
forward = string
206+
whitelisted_names = optional(list(string))
207+
})
208+
headers = optional(list(string))
209+
query_string = bool
210+
query_string_cache_keys = optional(list(string))
211+
}))
212+
lambda_function_association = optional(map(object({
213+
# event_type = map key
214+
lambda_arn = string
215+
include_body = optional(bool)
216+
})), {})
217+
function_association = optional(map(object({
218+
# event_type = map key
219+
function_arn = string
220+
})), {})
221+
max_ttl = optional(number)
222+
min_ttl = optional(number)
223+
origin_request_policy_id = optional(string)
224+
realtime_log_config_arn = optional(string)
225+
response_headers_policy_id = optional(string)
226+
smooth_streaming = optional(bool)
227+
target_origin_id = string
228+
trusted_key_groups = optional(list(string))
229+
trusted_signers = optional(list(string))
230+
viewer_protocol_policy = string
231+
grpc_config = optional(object({
232+
enabled = bool
233+
}))
234+
})
198235
}
199236

200237
variable "ordered_cache_behavior" {

wrappers/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module "wrapper" {
1212
create_origin_access_identity = try(each.value.create_origin_access_identity, var.defaults.create_origin_access_identity, false)
1313
create_vpc_origin = try(each.value.create_vpc_origin, var.defaults.create_vpc_origin, false)
1414
custom_error_response = try(each.value.custom_error_response, var.defaults.custom_error_response, {})
15-
default_cache_behavior = try(each.value.default_cache_behavior, var.defaults.default_cache_behavior, null)
15+
default_cache_behavior = try(each.value.default_cache_behavior, var.defaults.default_cache_behavior)
1616
default_root_object = try(each.value.default_root_object, var.defaults.default_root_object, null)
1717
enabled = try(each.value.enabled, var.defaults.enabled, true)
1818
geo_restriction = try(each.value.geo_restriction, var.defaults.geo_restriction, {})

0 commit comments

Comments
 (0)