Skip to content

Commit 58e7e6c

Browse files
Merge pull request #9 from TechHoldingLLC/fix/ordered-cache-ttl-values
Fix: Ordered cache TTL values
2 parents 8a69667 + 90eeb25 commit 58e7e6c

3 files changed

Lines changed: 161 additions & 67 deletions

File tree

EXAMPLE.md

Lines changed: 140 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ Below is an examples of calling this module.
55
```
66
module "cloudfront" {
77
source = "./cloudfront"
8-
origin = {
9-
domain_name = "s3_bucket_regional_domain_name"
10-
origin_id = "s3_bucket_name"
11-
12-
## We can only use Any one of Origin Access Control or Origin Access Identity
13-
# For Origin Access Control
14-
origin_access_control_id = "s3_cloudfront_origin_access_control_id"
15-
# For Origin Access Identity
16-
s3_origin_config = {
17-
s3_origin_access_identity = "s3_cloudfront_origin_access_identity_path"
18-
}
8+
origin = [
9+
{
10+
domain_name = "s3_bucket_regional_domain_name"
11+
origin_id = "s3_bucket_name"
1912
20-
}
13+
## We can only use Any one of Origin Access Control or Origin Access Identity
14+
# For Origin Access Control
15+
origin_access_control_id = "s3_cloudfront_origin_access_control_id"
16+
# For Origin Access Identity
17+
s3_origin_config = {
18+
s3_origin_access_identity = "s3_cloudfront_origin_access_identity_path"
19+
}
20+
21+
}
22+
]
2123
domain_aliases = ["example.com", "www.example.com"]
2224
acm_arn = "acm_arn"
2325
}
@@ -27,62 +29,67 @@ module "cloudfront" {
2729
```
2830
module "cloudfront" {
2931
source = "./cloudfront"
30-
origin = {
31-
domain_name = "s3_bucket_regional_domain_name"
32-
origin_id = "s3_bucket_name"
32+
origin = [
33+
{
34+
domain_name = "s3_bucket_regional_domain_name"
35+
origin_id = "s3_bucket_name"
3336
34-
## for http endpoint
35-
custom_origin_config = {
36-
origin_protocol_policy = "http-only"
37-
}
38-
## for https endpoint
39-
# custom_origin_config = {
40-
# origin_protocol_policy = "https-only"
41-
# }
42-
43-
## We can restrict publically accessible endpoint by adding custom headers in request sends from cloudfront to custom origin endpoint and validate headers on origin endpoint side
44-
custom_header = [
45-
{
46-
name = "Referer"
47-
value = "https://example.com"
48-
},
49-
{
50-
name = "Referer"
51-
value = "https://www.example.com"
52-
}
53-
]
54-
55-
domain_aliases = ["example.com", "www.example.com"]
56-
acm_arn = "acm_arn"
57-
## it's helpful to handle 404 to redirect on index.html with 200 response for read based build
58-
custom_error_response = [
59-
{
60-
error_caching_min_ttl = 300
61-
error_code = 404
62-
response_code = 200
63-
response_page_path = "/index.html"
37+
## for http endpoint
38+
custom_origin_config = {
39+
origin_protocol_policy = "http-only"
6440
}
65-
]
41+
## for https endpoint
42+
# custom_origin_config = {
43+
# origin_protocol_policy = "https-only"
44+
# }
45+
46+
## We can restrict publically accessible endpoint by adding custom headers in request sends from cloudfront to custom origin endpoint and validate headers on origin endpoint side
47+
custom_header = [
48+
{
49+
name = "Referer"
50+
value = "https://example.com"
51+
},
52+
{
53+
name = "Referer"
54+
value = "https://www.example.com"
55+
}
56+
]
57+
}
58+
]
59+
60+
domain_aliases = ["example.com", "www.example.com"]
61+
acm_arn = "acm_arn"
62+
## it's helpful to handle 404 to redirect on index.html with 200 response for read based build
63+
custom_error_response = [
64+
{
65+
error_caching_min_ttl = 300
66+
error_code = 404
67+
response_code = 200
68+
response_page_path = "/index.html"
69+
}
70+
]
6671
}
6772
```
6873

6974
## Cloudfront distribution with s3 Origin with TTL value
7075
```
7176
module "cloudfront" {
7277
source = "./cloudfront"
73-
origin = {
74-
domain_name = "s3_bucket_regional_domain_name"
75-
origin_id = "s3_bucket_name"
76-
77-
## We can only use Any one of Origin Access Control or Origin Access Identity
78-
# For Origin Access Control
79-
origin_access_control_id = "s3_cloudfront_origin_access_control_id"
80-
# For Origin Access Identity
81-
s3_origin_config = {
82-
s3_origin_access_identity = "s3_cloudfront_origin_access_identity_path"
83-
}
78+
origin = [
79+
{
80+
domain_name = "s3_bucket_regional_domain_name"
81+
origin_id = "s3_bucket_name"
8482
85-
}
83+
## We can only use Any one of Origin Access Control or Origin Access Identity
84+
# For Origin Access Control
85+
origin_access_control_id = "s3_cloudfront_origin_access_control_id"
86+
# For Origin Access Identity
87+
s3_origin_config = {
88+
s3_origin_access_identity = "s3_cloudfront_origin_access_identity_path"
89+
}
90+
91+
}
92+
]
8693
domain_aliases = ["example.com", "www.example.com"]
8794
acm_arn = "acm_arn"
8895
@@ -94,4 +101,79 @@ module "cloudfront" {
94101
default_ttl = 3500 # default amount of time that you want objects to stay in cloudfront cache before it sends another request to origin
95102
}
96103
}
104+
```
105+
106+
## Cloudfront distribution with multiple origin and cache behavior
107+
```
108+
module "cloudfront" {
109+
source = "./cloudfront"
110+
origin = [
111+
{
112+
domain_name = "domain_name"
113+
origin_id = "origin_id"
114+
},
115+
{
116+
domain_name = "domain_name"
117+
origin_id = "origin_id"
118+
origin_path = "/origin_path"
119+
}
120+
]
121+
122+
domain_aliases = ["example.com", "www.example.com"]
123+
acm_arn = "acm_arn"
124+
125+
default_cache_behaviour_target_origin_id = default_cache_behaviour_target_origin_id
126+
allowed_methods = ["list of allowed methods"]
127+
cache_policy_id = aws_managed_cache_policy_id
128+
129+
## Can be used only if cache_policy_id is not used
130+
# forwarded_values = {
131+
# query_string = true
132+
# query_string_cache_keys = ["list of query string cache keys"] # set only if query_string is true and not all query string are meant to be cached
133+
# headers = ["list of headers"] # specify * to include all headers
134+
# cookie_forward = ""
135+
# cookies_whitelisted_names = ["list of whitelisted cookie names"] # specify only if cookie forward is set to whitelist
136+
# }
137+
138+
## Can be used only if cache_policy_id is not used
139+
# ttl_values = {
140+
# min_ttl = 0
141+
# max_ttl = 31536000
142+
# default_ttl = 86400
143+
# }
144+
145+
ordered_cache_behavior = [
146+
{
147+
path_pattern = "path_pattern"
148+
target_origin_id = origin_id
149+
150+
ttl_values = {
151+
min_ttl = 0
152+
max_ttl = 31536000
153+
default_ttl = 86400
154+
}
155+
156+
forwarded_values = {
157+
query_string = true
158+
}
159+
160+
## Used to associate a cloudfront_function
161+
function_association = [
162+
{
163+
event_type = "event_type"
164+
function_arn = cloudfront_function_arn
165+
}
166+
]
167+
168+
## Used to associate a lambda_function
169+
lambda_function_association = [
170+
{
171+
event_type = "event_type"
172+
lambda_arn = lambda_function_arn
173+
include_body = true
174+
}
175+
]
176+
}
177+
]
178+
}
97179
```

cloudfront.tf

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,16 @@ resource "aws_cloudfront_distribution" "cloudfront" {
6767
default_ttl = lookup(var.ttl_values, "default_ttl", null)
6868

6969
dynamic "forwarded_values" {
70-
for_each = var.cache_policy_id != "" ? [] : [1]
70+
for_each = var.cache_policy_id != "" ? [] : [var.default_cache_forwarded_values]
7171

7272
content {
73-
query_string = false
73+
query_string = lookup(forwarded_values.value, "query_string", false)
74+
query_string_cache_keys = lookup(forwarded_values.value, "query_string_cache_keys", [])
75+
headers = lookup(forwarded_values.value, "headers", [])
7476

7577
cookies {
76-
forward = "none"
78+
forward = lookup(forwarded_values.value, "cookies_forward", "none")
79+
whitelisted_names = lookup(forwarded_values.value, "cookies_whitelisted_names", [])
7780
}
7881
}
7982
}
@@ -110,18 +113,21 @@ resource "aws_cloudfront_distribution" "cloudfront" {
110113

111114
viewer_protocol_policy = lookup(ordered_cache_behavior.value, "viewer_protocol_policy", "redirect-to-https")
112115
compress = lookup(ordered_cache_behavior.value, "compress", false)
113-
min_ttl = lookup(var.ttl_values, "min_ttl", null)
114-
max_ttl = lookup(var.ttl_values, "max_ttl", null)
115-
default_ttl = lookup(var.ttl_values, "default_ttl", null)
116+
min_ttl = lookup(ordered_cache_behavior.value.ttl_values, "min_ttl", null)
117+
max_ttl = lookup(ordered_cache_behavior.value.ttl_values, "max_ttl", null)
118+
default_ttl = lookup(ordered_cache_behavior.value.ttl_values, "default_ttl", null)
116119

117120
dynamic "forwarded_values" {
118-
for_each = contains(keys(ordered_cache_behavior.value), "cache_policy_id") ? [] : [1]
121+
for_each = contains(keys(ordered_cache_behavior.value), "cache_policy_id") ? [] : [ordered_cache_behavior.value.forwarded_values]
119122

120123
content {
121-
query_string = false
124+
query_string = lookup(forwarded_values.value, "query_string", false)
125+
query_string_cache_keys = lookup(forwarded_values.value, "query_string_cache_keys", [])
126+
headers = lookup(forwarded_values.value, "headers", [])
122127

123128
cookies {
124-
forward = "none"
129+
forward = lookup(forwarded_values.value, "cookies_forward", "none")
130+
whitelisted_names = lookup(forwarded_values.value, "cookies_whitelisted_names", [])
125131
}
126132
}
127133
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,10 @@ variable "ordered_cache_behavior" {
131131
description = "List of ordered cache behaviour"
132132
type = any
133133
default = []
134+
}
135+
136+
variable "default_cache_forwarded_values" {
137+
description = "forwarded values for default cache behavior"
138+
type = any
139+
default = []
134140
}

0 commit comments

Comments
 (0)