Skip to content

Commit faf66d1

Browse files
authored
add-caching-to-cdn (#24)
1 parent d880b7e commit faf66d1

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ The following inputs can be used as `step.with` keys
197197
| `aws_site_cdn_aliases` | String | Extra CNAMEs (alternate domain names), if any, for this distribution. Defaults to defined domain if none passed. (See note). |
198198
| `aws_site_cdn_custom_error_codes` | JSON | Custom error codes to define in CDN. Like `[{\"error_caching_min_ttl\":\"0\",\"error_code\":\"403\",\"response_code\":\"200\",\"response_page_path\":\"/index.html\"}]`. See [this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution.html#custom-error-response-arguments). |
199199
| `aws_site_cdn_response_headers_policy_id` | String | Comma separated list of response headers policy IDs for CloudFront. Eg. `Managed-CORS-with-preflight-and-SecurityHeadersPolicy` is `eaab4381-ed33-4a86-88ca-d9558dc6cd63`. |
200+
| `aws_site_cdn_min_ttl` | Number | Minimum TTL (in seconds) for CloudFront cache. Default is `0`. |
201+
| `aws_site_cdn_default_ttl` | Number | Default TTL (in seconds) for CloudFront cache. (CloudFront default is `86400` - 24 hours), but defaults to `0` (disabled) |
202+
| `aws_site_cdn_max_ttl` | Number | Maximum TTL (in seconds) for CloudFront cache. (CloudFront default is `31536000` 365 days), but defaults to `0` (disabled). |
200203
<hr/>
201204
<br/>
202205

@@ -243,6 +246,23 @@ If that's the case, `aws_site_cdn_aliases` should be set to: `site.bitovi.com,si
243246

244247
If they alternate domain names are child of the same domain, you can use a root cert for both.
245248

249+
## CloudFront Caching
250+
251+
For deployments or applications that rotate files on each deployment, the default cache TTL settings help ensure a smooth transition between deployments:
252+
253+
- **`aws_site_cdn_default_ttl`**: Set to 24 hours (86400 seconds)
254+
- **`aws_site_cdn_max_ttl`**: Set to 365 days (31536000 seconds)
255+
256+
These settings allow CloudFront to cache files even after they've been deleted from S3, reducing 404 errors during deployment transitions. Files remain cached at edge locations for the specified TTL period, giving users time to gradually transition to the new version.
257+
258+
Example with custom TTL settings:
259+
```yaml
260+
aws_site_cdn_enabled: true
261+
aws_site_cdn_min_ttl: 0
262+
aws_site_cdn_default_ttl: 172800 # 48 hours
263+
aws_site_cdn_max_ttl: 604800 # 7 days
264+
```
265+
246266
## Contributing
247267
We would love for you to contribute to [bitovi/github-actions-deploy-static-site-to-aws](https://github.com/bitovi/github-actions-deploy-static-site-to-aws).
248268
Would you like to see additional features? [Create an issue](https://github.com/bitovi/github-actions-deploy-static-site-to-aws/issues/new) or a [Pull Requests](https://github.com/bitovi/github-actions-deploy-static-site-to-aws/pulls). We love discussing solutions!

action.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ inputs:
7676
aws_site_cdn_response_headers_policy_id:
7777
description: 'Comma separated list of response headers policy IDs for CloudFront. Eg. Managed-CORS-with-preflight-and-SecurityHeadersPolicy is 67f7725c-6f97-4210-82d7-5512b31e9d42.'
7878
required: false
79+
aws_site_cdn_min_ttl:
80+
description: 'Minimum TTL (in seconds) for CloudFront cache. Default is 0.'
81+
required: false
82+
aws_site_cdn_default_ttl:
83+
description: 'Default TTL (in seconds) for CloudFront cache. Default is 86400 (24 hours).'
84+
required: false
85+
aws_site_cdn_max_ttl:
86+
description: 'Maximum TTL (in seconds) for CloudFront cache. Default is 31536000 (365 days).'
87+
required: false
7988

8089
# AWS Route53 Domains and Certificates
8190
aws_r53_domain_name:
@@ -140,6 +149,9 @@ runs:
140149
AWS_SITE_CDN_ALIASES: ${{ inputs.aws_site_cdn_aliases }}
141150
AWS_SITE_CDN_CUSTOM_ERROR_CODES: ${{ inputs.aws_site_cdn_custom_error_codes }}
142151
AWS_SITE_CDN_RESPONSE_HEADERS_POLICY_ID: ${{ inputs.aws_site_cdn_response_headers_policy_id }}
152+
AWS_SITE_CDN_MIN_TTL: ${{ inputs.aws_site_cdn_min_ttl }}
153+
AWS_SITE_CDN_DEFAULT_TTL: ${{ inputs.aws_site_cdn_default_ttl }}
154+
AWS_SITE_CDN_MAX_TTL: ${{ inputs.aws_site_cdn_max_ttl }}
143155
# AWS Route53 Domains abd Certificates
144156
AWS_R53_DOMAIN_NAME: ${{ inputs.aws_r53_domain_name }}
145157
AWS_R53_SUB_DOMAIN_NAME: ${{ inputs.aws_r53_sub_domain_name }}

scripts/generate_deploy.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ aws_site_cdn_enabled=$(generate_var aws_site_cdn_enabled $AWS_SITE_CDN_ENABLED)
9898
aws_site_cdn_aliases=$(generate_var aws_site_cdn_aliases $AWS_SITE_CDN_ALIASES)
9999
aws_site_cdn_custom_error_codes=$(generate_var aws_site_cdn_custom_error_codes $AWS_SITE_CDN_CUSTOM_ERROR_CODES)
100100
aws_site_cdn_response_headers_policy_id=$(generate_var aws_site_cdn_response_headers_policy_id $AWS_SITE_CDN_RESPONSE_HEADERS_POLICY_ID)
101+
aws_site_cdn_min_ttl=$(generate_var aws_site_cdn_min_ttl $AWS_SITE_CDN_MIN_TTL)
102+
aws_site_cdn_default_ttl=$(generate_var aws_site_cdn_default_ttl $AWS_SITE_CDN_DEFAULT_TTL)
103+
aws_site_cdn_max_ttl=$(generate_var aws_site_cdn_max_ttl $AWS_SITE_CDN_MAX_TTL)
101104
aws_site_root_object=$(generate_var aws_site_root_object $AWS_SITE_ROOT_OBJECT)
102105
aws_site_error_document=$(generate_var aws_site_error_document $AWS_SITE_ERROR_DOCUMENT)
103106
aws_r53_domain_name=$(generate_var aws_r53_domain_name $AWS_R53_DOMAIN_NAME)
@@ -121,6 +124,9 @@ $aws_site_cdn_enabled
121124
$aws_site_cdn_aliases
122125
$aws_site_cdn_custom_error_codes
123126
$aws_site_cdn_response_headers_policy_id
127+
$aws_site_cdn_min_ttl
128+
$aws_site_cdn_default_ttl
129+
$aws_site_cdn_max_ttl
124130
$aws_site_root_object
125131
$aws_site_error_document
126132
$aws_r53_domain_name

terraform_code/main.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ resource "aws_cloudfront_distribution" "cdn_static_site_default_cert" {
156156
}
157157

158158
default_cache_behavior {
159-
min_ttl = 0
160-
default_ttl = 0
161-
max_ttl = 0
159+
min_ttl = var.aws_site_cdn_min_ttl
160+
default_ttl = var.aws_site_cdn_default_ttl
161+
max_ttl = var.aws_site_cdn_max_ttl
162162
viewer_protocol_policy = "redirect-to-https"
163163

164164
allowed_methods = ["GET", "HEAD", "OPTIONS"]
@@ -212,9 +212,9 @@ resource "aws_cloudfront_distribution" "cdn_static_site" {
212212
}
213213

214214
default_cache_behavior {
215-
min_ttl = 0
216-
default_ttl = 0
217-
max_ttl = 0
215+
min_ttl = var.aws_site_cdn_min_ttl
216+
default_ttl = var.aws_site_cdn_default_ttl
217+
max_ttl = var.aws_site_cdn_max_ttl
218218
viewer_protocol_policy = "redirect-to-https"
219219

220220
allowed_methods = ["GET", "HEAD", "OPTIONS"]

terraform_code/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ variable "aws_site_cdn_response_headers_policy_id" {
6767
default = ""
6868
}
6969

70+
variable "aws_site_cdn_min_ttl" {
71+
description = "Minimum amount of time (in seconds) that objects stay in CloudFront cache before CloudFront forwards another request to the origin. Default is 0."
72+
type = number
73+
default = 0
74+
}
75+
76+
variable "aws_site_cdn_default_ttl" {
77+
description = "Default amount of time (in seconds) that an object is in a CloudFront cache before CloudFront forwards another request to the origin. Default is 86400 (24 hours)."
78+
type = number
79+
default = 0
80+
}
81+
82+
variable "aws_site_cdn_max_ttl" {
83+
description = "Maximum amount of time (in seconds) that an object is in a CloudFront cache before CloudFront forwards another request to the origin. Default is 31536000 (365 days)."
84+
type = number
85+
default = 0
86+
}
87+
7088
variable "aws_r53_domain_name" {
7189
description = "root domain name without any subdomains"
7290
type = string

0 commit comments

Comments
 (0)