fix: Add sane defaults to `custom_origins` and `ordered_cache` objects @jwadolowski (#147)
## what- All origins and behaviors should benefit from a set of sane defaults (there's no reason to treat custom ones differently)
- Align default timeout values with the ones used by
aws_cloudfront_distribution
- Streamline the upgrade process by removing the need to define all newly introduced variables (see below for details)
why
There was a discrepancy between default and custom origins/behaviors. The default items benefit from predefined variable values, so you can keep the module instance concise if you're ok with the defaults. Unfortunately, that applies neither to custom_origins
nor ordered_cache
objects, which results in a quite verbose module definition:
module "cdn" {
source = "cloudposse/cloudfront-cdn/aws"
version = "1.2.0"
# ...
custom_origins = [
{
origin_id = local.origin_id
domain_name = local.origin_domain_name
origin_path = ""
origin_access_control_id = null
custom_headers = []
custom_origin_config = null
}
]
ordered_cache = [
{
path_pattern = "/foo/bar/*"
target_origin_id = local.origin_id
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
cache_policy_id = aws_cloudfront_cache_policy.default.id
origin_request_policy_id = ""
compress = true
viewer_protocol_policy = "redirect-to-https"
response_headers_policy_id = aws_cloudfront_response_headers_policy.default.id
min_ttl = 0
default_ttl = 0
max_ttl = 0
forward_query_string = false
forward_cookies = "none"
forward_header_values = []
lambda_function_association = []
function_association = []
}
]
}
Since most of the object-scoped variables reference a default/empty/null-ish value, it could have been greatly reduced:
module "cdn" {
source = "cloudposse/cloudfront-cdn/aws"
version = "1.2.0"
# ...
custom_origins = [
{
origin_id = local.origin_id
domain_name = local.origin_domain_name
}
]
ordered_cache = [
{
path_pattern = "/foo/bar/*"
target_origin_id = local.origin_id
allowed_methods = ["GET", "HEAD"]
cache_policy_id = aws_cloudfront_cache_policy.default.id
compress = true
viewer_protocol_policy = "redirect-to-https"
response_headers_policy_id = aws_cloudfront_response_headers_policy.default.id
}
]
}
Additionally, whenever a new variable is introduced (e.g. #140 added origin_shield
), all of a sudden all existing custom origins need to be modified (by adding origin_shield = null
) to make them compliant and make the upgrade possible. Here's an example of before and after state:
module "cdn" {
source = "cloudposse/cloudfront-cdn/aws"
version = "1.2.0"
# ...
custom_origins = [
{
origin_id = local.origin_id
domain_name = local.origin_domain_name
origin_path = ""
origin_access_control_id = null
custom_headers = []
custom_origin_config = null
}
]
}
module "cdn" {
source = "cloudposse/cloudfront-cdn/aws"
version = "1.3.0" # New version introduces required origin_shield variable for custom origins
# ...
custom_origins = [
{
origin_id = local.origin_id
domain_name = local.origin_domain_name
origin_path = ""
origin_access_control_id = null
custom_headers = []
custom_origin_config = null
origin_shield = null # This one has to be added as a part of 1.2.0 -> 1.3.0 upgrade
}
]
}