Skip to content

Commit 7f05884

Browse files
jwadolowskimilldr
andauthored
feat: Improve CDN origin control (#140)
* feat: Enable explicit origin type definiton The module is meant to be used with custom origins, but it may happen that your origin of choice is an S3 bucket that's not managed by yourself. `cloudposse/cloudfront-s3-cdn/aws` may seem to be a viable alternative (it supports pre-existing buckets), but there's an implicit assumption there - both the origin bucket and the CloudFront distribution that points to it should be deployed to the same AWS account. In order to secure CDN-to-S3 communication one may want to specify either OAI or OAC. Prior to this change, it was not possible to use OAI when S3 bucket domain name was used. * fix: Set OAC only when S3 type origin was used When user specifies an S3 domain name as the origin and attaches OAC to it, Terraform fails with the following error: Illegal configuration: The origin type and OAC origin type differ. * feat: Make sure origin shield can be specified for non-default origins At the moment origin shield can be configured only for the default origin. This commit enables the feature for all the remaining ones. * fix: Replace unsupported map() references `map()` function is no longer available * fix: Zero all TTL params when cache policy ID is configured When cache policy ID is specified, all TTL arguments should be zeroed, as the policy-scoped TTLs take precedence. If non-zero `min_ttl`, `default_ttl` or `max_ttl` is set to non-0 value and at the same time cache policy is specified it leads to phantom changes in the plan: default_ttl = 0 -> 60 * Revert "fix: Zero all TTL params when cache policy ID is configured" This reverts commit 30ff7b9. This should be moved to a separate PR to narrow down the scope just to origin-related changes * docs: Reflect new/updated variables in the README file * fix: Fallback to default OAI when S3 origin was specified without `s3_origin_config` variable * feat: Add S3-specific examples * chore: Add an explicit comment to OAI resource * chore: Update OAI name * chore: Update s3_origin_config variable description * feat: Init s3_origin_config block only when OAC wasn't provided * fix: Improve S3 examples * chore: Resource naming convention --------- Co-authored-by: Dan Miller <[email protected]>
1 parent 8b13bb7 commit 7f05884

File tree

10 files changed

+447
-17
lines changed

10 files changed

+447
-17
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Available targets:
151151
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br/>See description of individual variables for details.<br/>Leave string and numeric variables as `null` to use default value.<br/>Individual variable settings (non-null) override settings in context object,<br/>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br/> "additional_tag_map": {},<br/> "attributes": [],<br/> "delimiter": null,<br/> "descriptor_formats": {},<br/> "enabled": true,<br/> "environment": null,<br/> "id_length_limit": null,<br/> "label_key_case": null,<br/> "label_order": [],<br/> "label_value_case": null,<br/> "labels_as_tags": [<br/> "unset"<br/> ],<br/> "name": null,<br/> "namespace": null,<br/> "regex_replace_chars": null,<br/> "stage": null,<br/> "tags": {},<br/> "tenant": null<br/>}</pre> | no |
152152
| <a name="input_custom_error_response"></a> [custom\_error\_response](#input\_custom\_error\_response) | List of one or more custom error response element maps | <pre>list(object({<br/> error_caching_min_ttl = string<br/> error_code = string<br/> response_code = string<br/> response_page_path = string<br/> }))</pre> | `[]` | no |
153153
| <a name="input_custom_header"></a> [custom\_header](#input\_custom\_header) | List of one or more custom headers passed to the origin | <pre>list(object({<br/> name = string<br/> value = string<br/> }))</pre> | `[]` | no |
154-
| <a name="input_custom_origins"></a> [custom\_origins](#input\_custom\_origins) | One or more custom origins for this distribution (multiples allowed). See documentation for configuration options description https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html#origin-arguments | <pre>list(object({<br/> domain_name = string<br/> origin_id = string<br/> origin_path = string<br/> origin_access_control_id = string<br/> custom_headers = list(object({<br/> name = string<br/> value = string<br/> }))<br/> custom_origin_config = object({<br/> http_port = number<br/> https_port = number<br/> origin_protocol_policy = string<br/> origin_ssl_protocols = list(string)<br/> origin_keepalive_timeout = number<br/> origin_read_timeout = number<br/> })<br/> s3_origin_config = object({<br/> origin_access_identity = string<br/> })<br/> }))</pre> | `[]` | no |
154+
| <a name="input_custom_origins"></a> [custom\_origins](#input\_custom\_origins) | One or more custom origins for this distribution (multiples allowed). See documentation for configuration options description https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html#origin-arguments | <pre>list(object({<br/> domain_name = string<br/> origin_id = string<br/> origin_path = string<br/> origin_access_control_id = string<br/> custom_headers = list(object({<br/> name = string<br/> value = string<br/> }))<br/> custom_origin_config = object({<br/> http_port = number<br/> https_port = number<br/> origin_protocol_policy = string<br/> origin_ssl_protocols = list(string)<br/> origin_keepalive_timeout = number<br/> origin_read_timeout = number<br/> })<br/> s3_origin_config = object({<br/> origin_access_identity = string<br/> })<br/> origin_shield = object({<br/> enabled = bool<br/> region = string<br/> })<br/> }))</pre> | `[]` | no |
155155
| <a name="input_default_root_object"></a> [default\_root\_object](#input\_default\_root\_object) | Object that CloudFront return when requests the root URL | `string` | `"index.html"` | no |
156156
| <a name="input_default_ttl"></a> [default\_ttl](#input\_default\_ttl) | Default amount of time (in seconds) that an object is in a CloudFront cache | `number` | `60` | no |
157157
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br/>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
@@ -201,12 +201,14 @@ Available targets:
201201
| <a name="input_origin_request_policy_id"></a> [origin\_request\_policy\_id](#input\_origin\_request\_policy\_id) | ID of the origin request policy attached to the cache behavior | `string` | `null` | no |
202202
| <a name="input_origin_shield"></a> [origin\_shield](#input\_origin\_shield) | The CloudFront Origin Shield settings | <pre>object({<br/> enabled = bool<br/> region = string<br/> })</pre> | `null` | no |
203203
| <a name="input_origin_ssl_protocols"></a> [origin\_ssl\_protocols](#input\_origin\_ssl\_protocols) | The SSL/TLS protocols that you want CloudFront to use when communicating with your origin over HTTPS | `list(string)` | <pre>[<br/> "TLSv1",<br/> "TLSv1.1",<br/> "TLSv1.2"<br/>]</pre> | no |
204+
| <a name="input_origin_type"></a> [origin\_type](#input\_origin\_type) | The type of origin configuration to use. Valid values are 'custom' or 's3'. | `string` | `"custom"` | no |
204205
| <a name="input_parent_zone_id"></a> [parent\_zone\_id](#input\_parent\_zone\_id) | ID of the hosted zone to contain this record (or specify `parent_zone_name`) | `string` | `""` | no |
205206
| <a name="input_parent_zone_name"></a> [parent\_zone\_name](#input\_parent\_zone\_name) | Name of the hosted zone to contain this record (or specify `parent_zone_id`) | `string` | `""` | no |
206207
| <a name="input_price_class"></a> [price\_class](#input\_price\_class) | Price class for this distribution: `PriceClass_All`, `PriceClass_200`, `PriceClass_100` | `string` | `"PriceClass_100"` | no |
207208
| <a name="input_realtime_log_config_arn"></a> [realtime\_log\_config\_arn](#input\_realtime\_log\_config\_arn) | The ARN of the real-time log configuration that is attached to this cache behavior | `string` | `null` | no |
208209
| <a name="input_regex_replace_chars"></a> [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.<br/>Characters matching the regex will be removed from the ID elements.<br/>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
209210
| <a name="input_response_headers_policy_id"></a> [response\_headers\_policy\_id](#input\_response\_headers\_policy\_id) | The identifier for a response headers policy | `string` | `""` | no |
211+
| <a name="input_s3_origin_config"></a> [s3\_origin\_config](#input\_s3\_origin\_config) | Optional configuration for an S3 origin. | <pre>object({<br/> origin_access_identity = string<br/> })</pre> | `null` | no |
210212
| <a name="input_stage"></a> [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
211213
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).<br/>Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no |
212214
| <a name="input_tenant"></a> [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no |

docs/terraform.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br/>See description of individual variables for details.<br/>Leave string and numeric variables as `null` to use default value.<br/>Individual variable settings (non-null) override settings in context object,<br/>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br/> "additional_tag_map": {},<br/> "attributes": [],<br/> "delimiter": null,<br/> "descriptor_formats": {},<br/> "enabled": true,<br/> "environment": null,<br/> "id_length_limit": null,<br/> "label_key_case": null,<br/> "label_order": [],<br/> "label_value_case": null,<br/> "labels_as_tags": [<br/> "unset"<br/> ],<br/> "name": null,<br/> "namespace": null,<br/> "regex_replace_chars": null,<br/> "stage": null,<br/> "tags": {},<br/> "tenant": null<br/>}</pre> | no |
4646
| <a name="input_custom_error_response"></a> [custom\_error\_response](#input\_custom\_error\_response) | List of one or more custom error response element maps | <pre>list(object({<br/> error_caching_min_ttl = string<br/> error_code = string<br/> response_code = string<br/> response_page_path = string<br/> }))</pre> | `[]` | no |
4747
| <a name="input_custom_header"></a> [custom\_header](#input\_custom\_header) | List of one or more custom headers passed to the origin | <pre>list(object({<br/> name = string<br/> value = string<br/> }))</pre> | `[]` | no |
48-
| <a name="input_custom_origins"></a> [custom\_origins](#input\_custom\_origins) | One or more custom origins for this distribution (multiples allowed). See documentation for configuration options description https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html#origin-arguments | <pre>list(object({<br/> domain_name = string<br/> origin_id = string<br/> origin_path = string<br/> origin_access_control_id = string<br/> custom_headers = list(object({<br/> name = string<br/> value = string<br/> }))<br/> custom_origin_config = object({<br/> http_port = number<br/> https_port = number<br/> origin_protocol_policy = string<br/> origin_ssl_protocols = list(string)<br/> origin_keepalive_timeout = number<br/> origin_read_timeout = number<br/> })<br/> s3_origin_config = object({<br/> origin_access_identity = string<br/> })<br/> }))</pre> | `[]` | no |
48+
| <a name="input_custom_origins"></a> [custom\_origins](#input\_custom\_origins) | One or more custom origins for this distribution (multiples allowed). See documentation for configuration options description https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html#origin-arguments | <pre>list(object({<br/> domain_name = string<br/> origin_id = string<br/> origin_path = string<br/> origin_access_control_id = string<br/> custom_headers = list(object({<br/> name = string<br/> value = string<br/> }))<br/> custom_origin_config = object({<br/> http_port = number<br/> https_port = number<br/> origin_protocol_policy = string<br/> origin_ssl_protocols = list(string)<br/> origin_keepalive_timeout = number<br/> origin_read_timeout = number<br/> })<br/> s3_origin_config = object({<br/> origin_access_identity = string<br/> })<br/> origin_shield = object({<br/> enabled = bool<br/> region = string<br/> })<br/> }))</pre> | `[]` | no |
4949
| <a name="input_default_root_object"></a> [default\_root\_object](#input\_default\_root\_object) | Object that CloudFront return when requests the root URL | `string` | `"index.html"` | no |
5050
| <a name="input_default_ttl"></a> [default\_ttl](#input\_default\_ttl) | Default amount of time (in seconds) that an object is in a CloudFront cache | `number` | `60` | no |
5151
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br/>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
@@ -95,12 +95,14 @@
9595
| <a name="input_origin_request_policy_id"></a> [origin\_request\_policy\_id](#input\_origin\_request\_policy\_id) | ID of the origin request policy attached to the cache behavior | `string` | `null` | no |
9696
| <a name="input_origin_shield"></a> [origin\_shield](#input\_origin\_shield) | The CloudFront Origin Shield settings | <pre>object({<br/> enabled = bool<br/> region = string<br/> })</pre> | `null` | no |
9797
| <a name="input_origin_ssl_protocols"></a> [origin\_ssl\_protocols](#input\_origin\_ssl\_protocols) | The SSL/TLS protocols that you want CloudFront to use when communicating with your origin over HTTPS | `list(string)` | <pre>[<br/> "TLSv1",<br/> "TLSv1.1",<br/> "TLSv1.2"<br/>]</pre> | no |
98+
| <a name="input_origin_type"></a> [origin\_type](#input\_origin\_type) | The type of origin configuration to use. Valid values are 'custom' or 's3'. | `string` | `"custom"` | no |
9899
| <a name="input_parent_zone_id"></a> [parent\_zone\_id](#input\_parent\_zone\_id) | ID of the hosted zone to contain this record (or specify `parent_zone_name`) | `string` | `""` | no |
99100
| <a name="input_parent_zone_name"></a> [parent\_zone\_name](#input\_parent\_zone\_name) | Name of the hosted zone to contain this record (or specify `parent_zone_id`) | `string` | `""` | no |
100101
| <a name="input_price_class"></a> [price\_class](#input\_price\_class) | Price class for this distribution: `PriceClass_All`, `PriceClass_200`, `PriceClass_100` | `string` | `"PriceClass_100"` | no |
101102
| <a name="input_realtime_log_config_arn"></a> [realtime\_log\_config\_arn](#input\_realtime\_log\_config\_arn) | The ARN of the real-time log configuration that is attached to this cache behavior | `string` | `null` | no |
102103
| <a name="input_regex_replace_chars"></a> [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.<br/>Characters matching the regex will be removed from the ID elements.<br/>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
103104
| <a name="input_response_headers_policy_id"></a> [response\_headers\_policy\_id](#input\_response\_headers\_policy\_id) | The identifier for a response headers policy | `string` | `""` | no |
105+
| <a name="input_s3_origin_config"></a> [s3\_origin\_config](#input\_s3\_origin\_config) | Optional configuration for an S3 origin. | <pre>object({<br/> origin_access_identity = string<br/> })</pre> | `null` | no |
104106
| <a name="input_stage"></a> [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
105107
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).<br/>Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no |
106108
| <a name="input_tenant"></a> [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no |

0 commit comments

Comments
 (0)