Skip to content

Commit 1c01040

Browse files
committed
Allow specifying CloudFront ordered cache behaviours
1 parent f515c50 commit 1c01040

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Terraform module to host a static site generated by Publii
8585
| <a name="input_cloudfront_enable_apex_to_www_redirect"></a> [cloudfront\_enable\_apex\_to\_www\_redirect](#input\_cloudfront\_enable\_apex\_to\_www\_redirect) | Enable CloudFront apex to www redirect | `bool` | `true` | no |
8686
| <a name="input_cloudfront_enable_ipv6"></a> [cloudfront\_enable\_ipv6](#input\_cloudfront\_enable\_ipv6) | Enable IPv6 on CloudFront | `bool` | `true` | no |
8787
| <a name="input_cloudfront_enable_waf"></a> [cloudfront\_enable\_waf](#input\_cloudfront\_enable\_waf) | Enable CloudFront WAF | `bool` | `true` | no |
88+
| <a name="input_cloudfront_ordered_cache_behaviors"></a> [cloudfront\_ordered\_cache\_behaviors](#input\_cloudfront\_ordered\_cache\_behaviors) | List of ordered\_cache\_behavior objects. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#cache-behavior-arguments | `list(map(any))` | `[]` | no |
8889
| <a name="input_cloudfront_tls_certificate_arn"></a> [cloudfront\_tls\_certificate\_arn](#input\_cloudfront\_tls\_certificate\_arn) | CloudFront TLS certificate ARN (must be created in us-east-1 region) | `string` | `""` | no |
8990
| <a name="input_enable_publii_pretty_urls"></a> [enable\_publii\_pretty\_urls](#input\_enable\_publii\_pretty\_urls) | If you hae enabled 'Pretty URLs' in Publii, set this to true | `bool` | `false` | no |
9091
| <a name="input_route53_hosted_zone_options"></a> [route53\_hosted\_zone\_options](#input\_route53\_hosted\_zone\_options) | If you have a Route53 zone, the required DNS records can be created automatically. | <pre>object({<br> id = string<br> create_certificate_dns_validation_records = bool<br> create_site_url_dns_records = bool<br> })</pre> | <pre>{<br> "create_certificate_dns_validation_records": false,<br> "create_site_url_dns_records": false,<br> "id": ""<br>}</pre> | no |

cloudfront-frontend.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,72 @@ resource "aws_cloudfront_distribution" "frontend" {
5959
response_page_path = "/404.html"
6060
}
6161

62+
## borrowed/copied from https://github.com/terraform-aws-modules/terraform-aws-cloudfront/blob/master/main.tf
63+
dynamic "ordered_cache_behavior" {
64+
for_each = local.cloudfront_ordered_cache_behaviors
65+
iterator = i
66+
67+
content {
68+
path_pattern = i.value["path_pattern"]
69+
target_origin_id = i.value["target_origin_id"]
70+
viewer_protocol_policy = i.value["viewer_protocol_policy"]
71+
72+
allowed_methods = lookup(i.value, "allowed_methods", ["GET", "HEAD", "OPTIONS"])
73+
cached_methods = lookup(i.value, "cached_methods", ["GET", "HEAD"])
74+
compress = lookup(i.value, "compress", null)
75+
field_level_encryption_id = lookup(i.value, "field_level_encryption_id", null)
76+
smooth_streaming = lookup(i.value, "smooth_streaming", null)
77+
trusted_signers = lookup(i.value, "trusted_signers", null)
78+
trusted_key_groups = lookup(i.value, "trusted_key_groups", null)
79+
80+
cache_policy_id = lookup(i.value, "cache_policy_id", null)
81+
origin_request_policy_id = lookup(i.value, "origin_request_policy_id", null)
82+
response_headers_policy_id = lookup(i.value, "response_headers_policy_id", null)
83+
realtime_log_config_arn = lookup(i.value, "realtime_log_config_arn", null)
84+
85+
min_ttl = lookup(i.value, "min_ttl", null)
86+
default_ttl = lookup(i.value, "default_ttl", null)
87+
max_ttl = lookup(i.value, "max_ttl", null)
88+
89+
dynamic "forwarded_values" {
90+
for_each = lookup(i.value, "use_forwarded_values", true) ? [true] : []
91+
92+
content {
93+
query_string = lookup(i.value, "query_string", false)
94+
query_string_cache_keys = lookup(i.value, "query_string_cache_keys", [])
95+
headers = lookup(i.value, "headers", [])
96+
97+
cookies {
98+
forward = lookup(i.value, "cookies_forward", "none")
99+
whitelisted_names = lookup(i.value, "cookies_whitelisted_names", null)
100+
}
101+
}
102+
}
103+
104+
dynamic "lambda_function_association" {
105+
for_each = lookup(i.value, "lambda_function_association", [])
106+
iterator = l
107+
108+
content {
109+
event_type = l.key
110+
lambda_arn = l.value.lambda_arn
111+
include_body = lookup(l.value, "include_body", null)
112+
}
113+
}
114+
115+
dynamic "function_association" {
116+
for_each = lookup(i.value, "function_association", [])
117+
iterator = f
118+
119+
content {
120+
event_type = f.key
121+
function_arn = f.value.function_arn
122+
}
123+
}
124+
}
125+
}
126+
##
127+
62128
price_class = "PriceClass_100"
63129

64130
restrictions {

locals.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ locals {
88
cloudfront_enable_apex_to_www_redirect = var.cloudfront_enable_apex_to_www_redirect
99
enable_publii_pretty_urls = var.enable_publii_pretty_urls
1010
route53_hosted_zone_options = var.route53_hosted_zone_options
11+
cloudfront_ordered_cache_behaviors = var.cloudfront_ordered_cache_behaviors
1112
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ variable "cloudfront_enable_apex_to_www_redirect" {
3333
default = true
3434
}
3535

36+
variable "cloudfront_ordered_cache_behaviors" {
37+
description = "List of ordered_cache_behavior objects. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#cache-behavior-arguments"
38+
type = list(map(any))
39+
default = []
40+
}
41+
3642
variable "enable_publii_pretty_urls" {
3743
description = "If you hae enabled 'Pretty URLs' in Publii, set this to true"
3844
type = bool

0 commit comments

Comments
 (0)