Skip to content

Commit e0d9620

Browse files
dudymasmilldr
andauthored
feat: spa-s3-cloudfront creates cache policies (#1061)
Co-authored-by: Dan Miller <[email protected]>
1 parent eced166 commit e0d9620

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

modules/spa-s3-cloudfront/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ components:
165165

166166
| Name | Type |
167167
|------|------|
168+
| [aws_cloudfront_cache_policy.created_cache_policies](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_cache_policy) | resource |
169+
| [aws_cloudfront_origin_request_policy.created_origin_request_policies](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_origin_request_policy) | resource |
168170
| [aws_iam_policy.additional_lambda_edge_permission](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
169171
| [aws_iam_role.github_actions](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
170172
| [aws_iam_role_policy_attachment.additional_lambda_edge_permission](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
@@ -236,7 +238,7 @@ components:
236238
| <a name="input_lambda_edge_runtime"></a> [lambda\_edge\_runtime](#input\_lambda\_edge\_runtime) | The default Lambda@Edge runtime for all functions.<br><br>This value is deep merged in `module.lambda_edge_functions` with `var.lambda_edge_functions` and can be overwritten for any individual function. | `string` | `"nodejs16.x"` | no |
237239
| <a name="input_name"></a> [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.<br>This is the only ID element not also included as a `tag`.<br>The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no |
238240
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |
239-
| <a name="input_ordered_cache"></a> [ordered\_cache](#input\_ordered\_cache) | An ordered list of [cache behaviors](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#cache-behavior-arguments) resource for this distribution.<br>List in order of precedence (first match wins). This is in addition to the default cache policy.<br>Set `target_origin_id` to `""` to specify the S3 bucket origin created by this module. | <pre>list(object({<br> target_origin_id = string<br> path_pattern = string<br><br> allowed_methods = list(string)<br> cached_methods = list(string)<br> compress = bool<br> trusted_signers = list(string)<br> trusted_key_groups = list(string)<br><br> cache_policy_id = string<br> origin_request_policy_id = string<br><br> viewer_protocol_policy = string<br> min_ttl = number<br> default_ttl = number<br> max_ttl = number<br> response_headers_policy_id = string<br><br> forward_query_string = bool<br> forward_header_values = list(string)<br> forward_cookies = string<br> forward_cookies_whitelisted_names = list(string)<br><br> lambda_function_association = list(object({<br> event_type = string<br> include_body = bool<br> lambda_arn = string<br> }))<br><br> function_association = list(object({<br> event_type = string<br> function_arn = string<br> }))<br> }))</pre> | `[]` | no |
241+
| <a name="input_ordered_cache"></a> [ordered\_cache](#input\_ordered\_cache) | An ordered list of [cache behaviors](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#cache-behavior-arguments) resource for this distribution.<br>List in order of precedence (first match wins). This is in addition to the default cache policy.<br>Set `target_origin_id` to `""` to specify the S3 bucket origin created by this module.<br>Set `cache_policy_id` to `""` to use `cache_policy_name` for creating a new policy. At least one of the two must be set.<br>Set `origin_request_policy_id` to `""` to use `origin_request_policy_name` for creating a new policy. At least one of the two must be set. | <pre>list(object({<br> target_origin_id = string<br> path_pattern = string<br><br> allowed_methods = list(string)<br> cached_methods = list(string)<br> compress = bool<br> trusted_signers = list(string)<br> trusted_key_groups = list(string)<br><br> cache_policy_name = optional(string)<br> cache_policy_id = optional(string)<br> origin_request_policy_name = optional(string)<br> origin_request_policy_id = optional(string)<br><br> viewer_protocol_policy = string<br> min_ttl = number<br> default_ttl = number<br> max_ttl = number<br> response_headers_policy_id = string<br><br> forward_query_string = bool<br> forward_header_values = list(string)<br> forward_cookies = string<br> forward_cookies_whitelisted_names = list(string)<br><br> lambda_function_association = list(object({<br> event_type = string<br> include_body = bool<br> lambda_arn = string<br> }))<br><br> function_association = list(object({<br> event_type = string<br> function_arn = string<br> }))<br> }))</pre> | `[]` | no |
240242
| <a name="input_origin_allow_ssl_requests_only"></a> [origin\_allow\_ssl\_requests\_only](#input\_origin\_allow\_ssl\_requests\_only) | Set to `true` in order to have the origin bucket require requests to use Secure Socket Layer (HTTPS/SSL). This will explicitly deny access to HTTP requests | `bool` | `true` | no |
241243
| <a name="input_origin_deployment_actions"></a> [origin\_deployment\_actions](#input\_origin\_deployment\_actions) | List of actions to permit `origin_deployment_principal_arns` to perform on bucket and bucket prefixes (see `origin_deployment_principal_arns`) | `list(string)` | <pre>[<br> "s3:PutObject",<br> "s3:PutObjectAcl",<br> "s3:GetObject",<br> "s3:DeleteObject",<br> "s3:ListBucket",<br> "s3:ListBucketMultipartUploads",<br> "s3:GetBucketLocation",<br> "s3:AbortMultipartUpload"<br>]</pre> | no |
242244
| <a name="input_origin_deployment_principal_arns"></a> [origin\_deployment\_principal\_arns](#input\_origin\_deployment\_principal\_arns) | List of role ARNs to grant deployment permissions to the origin Bucket. | `list(string)` | `[]` | no |

modules/spa-s3-cloudfront/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ module "spa_web" {
106106
min_ttl = local.cloudfront_min_ttl
107107
max_ttl = local.cloudfront_max_ttl
108108

109-
ordered_cache = var.ordered_cache
109+
ordered_cache = local.ordered_cache
110110
forward_cookies = var.forward_cookies
111111
forward_header_values = local.forward_header_values
112112

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
resource "aws_cloudfront_cache_policy" "created_cache_policies" {
2+
for_each = {
3+
for cache in var.ordered_cache : cache.cache_policy_name => cache if cache.cache_policy_id == null
4+
}
5+
6+
comment = var.comment
7+
default_ttl = each.value.default_ttl
8+
max_ttl = each.value.max_ttl
9+
min_ttl = each.value.min_ttl
10+
name = each.value.cache_policy_name
11+
parameters_in_cache_key_and_forwarded_to_origin {
12+
cookies_config {
13+
cookie_behavior = "none"
14+
}
15+
headers_config {
16+
header_behavior = "none"
17+
}
18+
query_strings_config {
19+
query_string_behavior = "none"
20+
}
21+
}
22+
}
23+
24+
resource "aws_cloudfront_origin_request_policy" "created_origin_request_policies" {
25+
for_each = {
26+
for cache in var.ordered_cache : cache.origin_request_policy_name => cache if cache.origin_request_policy_id == null
27+
}
28+
29+
comment = var.comment
30+
name = each.value.origin_request_policy_name
31+
cookies_config {
32+
cookie_behavior = "none"
33+
}
34+
headers_config {
35+
header_behavior = "none"
36+
}
37+
query_strings_config {
38+
query_string_behavior = "none"
39+
}
40+
}
41+
42+
locals {
43+
ordered_cache = [
44+
for cache in var.ordered_cache : merge(cache, {
45+
cache_policy_id = cache.cache_policy_id == null ? aws_cloudfront_cache_policy.created_cache_policies[cache.cache_policy_name].id : cache.cache_policy_id
46+
origin_request_policy_id = cache.origin_request_policy_id == null ? aws_cloudfront_origin_request_policy.created_origin_request_policies[cache.origin_request_policy_name].id : cache.origin_request_policy_id
47+
})
48+
]
49+
}

modules/spa-s3-cloudfront/variables.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,10 @@ variable "ordered_cache" {
398398
trusted_signers = list(string)
399399
trusted_key_groups = list(string)
400400

401-
cache_policy_id = string
402-
origin_request_policy_id = string
401+
cache_policy_name = optional(string)
402+
cache_policy_id = optional(string)
403+
origin_request_policy_name = optional(string)
404+
origin_request_policy_id = optional(string)
403405

404406
viewer_protocol_policy = string
405407
min_ttl = number
@@ -428,6 +430,8 @@ variable "ordered_cache" {
428430
An ordered list of [cache behaviors](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#cache-behavior-arguments) resource for this distribution.
429431
List in order of precedence (first match wins). This is in addition to the default cache policy.
430432
Set `target_origin_id` to `""` to specify the S3 bucket origin created by this module.
433+
Set `cache_policy_id` to `""` to use `cache_policy_name` for creating a new policy. At least one of the two must be set.
434+
Set `origin_request_policy_id` to `""` to use `origin_request_policy_name` for creating a new policy. At least one of the two must be set.
431435
EOT
432436
}
433437

0 commit comments

Comments
 (0)