Skip to content

Commit 4d5f8a9

Browse files
committed
feat(distribution): improve error_responses variable
1 parent 4b90e1b commit 4d5f8a9

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

modules/distribution/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ This module creates following resources.
4343
| <a name="input_custom_origins"></a> [custom\_origins](#input\_custom\_origins) | (Optional) A configuration for custom origins of the distribution. Each key defines a name of each custom origin. Each value of `custom_origins` as defined below.<br/> (Required) `host` - The DNS domain name of either the web site of your custom origin.<br/> (Optional) `path` - The URL path to append to `host` which the origin domain name for origin requests. Enter the directory path, beginning with a slash (/). Do not add a slash (/) at the end of the path.<br/> (Optional) `http_port` - The HTTP port the custom origin listens on. Defaults to `80`.<br/> (Optional) `https_port` - The HTTPS port the custom origin listens on. Defaults to `443`.<br/> (Optional) `origin_access` - The configuration of origin access for the origin. `origin_access` block as defined below.<br/> (Optional) `type` - The type of origin access. Valid values are `CONTROL` and `NONE`. Defaults to `NONE`.<br/> (Optional) `id` - The ID of origin access control if `type` is `CONTROL`.<br/> (Optional) `protocol_policy` - The origin protocol policy to apply to your origin. The origin protocol policy determines the protocol (HTTP or HTTPS) that you want CloudFront to use when connecting to the origin. Valid values are `HTTP_ONLY`, `HTTPS_ONLY` or `MATCH_VIEWER`. Defaults to `MATCH_VIEWER`.<br/> (Optional) `ssl_security_policy` - The minimum SSL/TLS protocol that CloudFront uses with the origin over HTTPS. Valid values are `SSLv3`, `TLSv1`, `TLSv1.1`, and `TLSv1.2`. Defaults to `TLSv1.1`. Recommend the latest TLS protocol that the origin supports.<br/> (Optional) `custom_headers` - A map of custom HTTP headers to include in all requests to the origin. Each key/value is mapping to HTTP header `name`/`value`.<br/> (Optional) `origin_shield` - Origin Shield is an additional caching layer that can help reduce the load on your origin and help protect its availability. `origin_shield` block as defined below.<br/> (Required) `enabled` - Whether to enable Origin Shield. Defaults to `false`.<br/> (Required) `region` - The AWS Region for Origin Shield. To specify a region. For example, specify the US East (Ohio) region as `us-east-2`.<br/> (Optional) `connection_attempts` - The number of times that CloudFront attempts to connect to the origin, from `1` to `3`. Defaults to `3`.<br/> (Optional) `connection_timeout` - The number of seconds that CloudFront waits for a response from the origin, from `1` to `10`. Defaults to `10`.<br/> (Optional) `keepalive_timeout` - The number of seconds that CloudFront maintains an idle connection with the origin, from `1` to `60`. But, the maximum can be changed arbitrarily by AWS Support to a much higher value. Defaults to `5`.<br/> (Optional) `response_timeout` - The number of seconds that CloudFront waits for a response from the origin, from `1` to `60`. Defaults to `30`. | <pre>map(object({<br/> host = string<br/> path = optional(string)<br/> http_port = optional(number, 80)<br/> https_port = optional(number, 443)<br/> origin_access = optional(object({<br/> type = optional(string, "NONE")<br/> id = optional(string)<br/> }))<br/> protocol_policy = optional(string, "MATCH_VIEWER")<br/> ssl_security_policy = optional(string, "TLSv1.1")<br/> custom_headers = optional(map(string), {})<br/> origin_shield = optional(object({<br/> enabled = bool<br/> region = string<br/> }))<br/> connection_attempts = optional(number, 3)<br/> connection_timeout = optional(number, 10)<br/> keepalive_timeout = optional(number, 5)<br/> response_timeout = optional(number, 30)<br/> }))</pre> | `{}` | no |
4444
| <a name="input_description"></a> [description](#input\_description) | (Optional) The description of the distribution. Any comments you want to include about the distribution. | `string` | `"Managed by Terraform."` | no |
4545
| <a name="input_enabled"></a> [enabled](#input\_enabled) | (Optional) Whether the distribution is enabled to accept end user requests for content. Defaults to `true`. | `bool` | `true` | no |
46-
| <a name="input_error_responses"></a> [error\_responses](#input\_error\_responses) | (Optional) A configurations of custom error responses for the distribution. Each key means the HTTP status code that you want to customize like `404`, `503`. Each value of `error_responses` as defined below.<br/> (Optional) `cache_min_ttl` - The minimum TTL(Time-to-live) in seconds that you want HTTP error codes to stay in CloudFront caches before CloudFront queries your origin to see whether the object has been updated. Defaults to `10`.<br/> (Optional) `custom_response_code` - The HTTP status code to return to the viewer. CloudFront can return a different status code to the viewer than what it received from the origin.<br/> (Optional) `custom_response_path` - The path to the custom error response page. | `any` | `{}` | no |
46+
| <a name="input_error_responses"></a> [error\_responses](#input\_error\_responses) | (Optional) A configurations of custom error responses for the distribution. Each key means the HTTP status code that you want to customize like `404`, `503`. Each value of `error_responses` as defined below.<br/> (Optional) `cache_min_ttl` - The minimum TTL(Time-to-live) in seconds that you want HTTP error codes to stay in CloudFront caches before CloudFront queries your origin to see whether the object has been updated. Defaults to `10`.<br/> (Optional) `custom_response` - A configuration for custom error response. `custom_response` block as defined below.<br/> (Required) `status_code` - The HTTP status code that you want CloudFront to return to the viewer along with the custom error page. You must specify a value for `status_code`, even if it is the same value as the `error_code`.<br/> (Required) `path` - The path of the custom error page that you want CloudFront to return to a viewer when your origin returns the corresponding `error_code`. The path must begin with a slash (/). | <pre>map(object({<br/> cache_min_ttl = optional(number, 10)<br/> custom_response = optional(object({<br/> status_code = number<br/> path = string<br/> }))<br/> }))</pre> | `{}` | no |
4747
| <a name="input_geographic_restriction"></a> [geographic\_restriction](#input\_geographic\_restriction) | (Optional) A configuration for CloudFront geographic restrictions. `geographic_restriction` as defined below.<br/> (Optiona) `type` - The method that you want to use to restrict distribution of the content by country. Valid values are `NONE`, `WHITELIST` or `BLACKLIST`. Defaults to `NONE`.<br/> (Optiona) `countries` - A list of the ISO 3166-1-alpha-2 codes for which you want CloudFront either to distribute your content (`WHITELIST`) or not distribute your content (`BLACKLIST`). | <pre>object({<br/> type = optional(string, "NONE")<br/> countries = optional(set(string), [])<br/> })</pre> | `{}` | no |
4848
| <a name="input_http_version"></a> [http\_version](#input\_http\_version) | (Optional) The maximum HTTP version to support on the distribution. Valid values are `HTTP1.1`, `HTTP2`, `HTTP2AND3`, or `HTTP3`. Defaults to `HTTP2`. | `string` | `"HTTP2"` | no |
4949
| <a name="input_ipv6_enabled"></a> [ipv6\_enabled](#input\_ipv6\_enabled) | (Optional) Whether the IPv6 is enabled for the distribution. Defaults to `true`. | `bool` | `true` | no |

modules/distribution/main.tf

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,20 @@ resource "aws_cloudfront_distribution" "this" {
100100

101101
dynamic "custom_error_response" {
102102
for_each = var.error_responses
103+
iterator = response
103104

104105
content {
105-
error_code = custom_error_response.key
106-
error_caching_min_ttl = try(custom_error_response.value.cache_min_ttl, 10)
106+
error_code = response.key
107+
error_caching_min_ttl = response.value.cache_min_ttl
107108

108-
response_code = try(custom_error_response.value.custom_response_code, null)
109-
response_page_path = try(custom_error_response.value.custom_response_path, null)
109+
response_code = (response.value.custom_response != null
110+
? response.value.custom_response.status_code
111+
: null
112+
)
113+
response_page_path = (response.value.custom_response != null
114+
? response.value.custom_response.path
115+
: null
116+
)
110117
}
111118
}
112119

modules/distribution/outputs.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ output "error_responses" {
8585
response.error_code => {
8686
cache_min_ttl = response.error_caching_min_ttl
8787

88-
custom_response_code = response.response_code
89-
custom_response_path = response.response_page_path
88+
custom_response = (response.response_code != null
89+
? {
90+
status_code = response.response_code
91+
path = response.response_page_path
92+
}
93+
: null
94+
)
9095
}
9196
}
9297
}

modules/distribution/variables.tf

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,19 @@ variable "error_responses" {
102102
description = <<EOF
103103
(Optional) A configurations of custom error responses for the distribution. Each key means the HTTP status code that you want to customize like `404`, `503`. Each value of `error_responses` as defined below.
104104
(Optional) `cache_min_ttl` - The minimum TTL(Time-to-live) in seconds that you want HTTP error codes to stay in CloudFront caches before CloudFront queries your origin to see whether the object has been updated. Defaults to `10`.
105-
(Optional) `custom_response_code` - The HTTP status code to return to the viewer. CloudFront can return a different status code to the viewer than what it received from the origin.
106-
(Optional) `custom_response_path` - The path to the custom error response page.
105+
(Optional) `custom_response` - A configuration for custom error response. `custom_response` block as defined below.
106+
(Required) `status_code` - The HTTP status code that you want CloudFront to return to the viewer along with the custom error page. You must specify a value for `status_code`, even if it is the same value as the `error_code`.
107+
(Required) `path` - The path of the custom error page that you want CloudFront to return to a viewer when your origin returns the corresponding `error_code`. The path must begin with a slash (/).
107108
EOF
108-
type = any
109-
default = {}
110-
nullable = false
109+
type = map(object({
110+
cache_min_ttl = optional(number, 10)
111+
custom_response = optional(object({
112+
status_code = number
113+
path = string
114+
}))
115+
}))
116+
default = {}
117+
nullable = false
111118
}
112119

113120
variable "geographic_restriction" {

0 commit comments

Comments
 (0)