Skip to content

Commit dca9731

Browse files
Refactor to use managed cache policy
This avoids a super-long TTL on the woff2 files for now (setting to 24 hours), which matches the TTL used by CloudFront today. In general this should probably be enough for most purposes. We also add a stale-while-revalidate to let most users check the resource in the background, rather than waiting on the request to complete before proceeding.
1 parent db992c1 commit dca9731

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

terraform/releases/impl/cache.tf

Lines changed: 0 additions & 11 deletions
This file was deleted.

terraform/releases/impl/cloudfront-doc.tf

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ module "lambda_doc_router" {
1111
role_arn = data.aws_iam_role.cloudfront_lambda.arn
1212
}
1313

14+
resource "aws_cloudfront_response_headers_policy" "cache_immutable" {
15+
name = format("immutable-for-%s-releases", var.name)
16+
17+
custom_headers_config {
18+
items {
19+
header = "Cache-Control"
20+
override = true
21+
value = format(
22+
"immutable, max-age=%d, stale-while-revalidate=%d",
23+
data.aws_cloudfront_cache_policy.caching.default_ttl,
24+
data.aws_cloudfront_cache_policy.caching.default_ttl,
25+
)
26+
}
27+
}
28+
}
29+
30+
data "aws_cloudfront_cache_policy" "caching" {
31+
name = "Managed-CachingOptimized"
32+
}
33+
1434
resource "aws_cloudfront_distribution" "doc" {
1535
comment = var.doc_domain_name
1636

@@ -51,25 +71,21 @@ resource "aws_cloudfront_distribution" "doc" {
5171

5272
ordered_cache_behavior {
5373
path_pattern = "*.woff2"
54-
cache_policy_id = "cache-immutable"
5574
allowed_methods = ["GET", "HEAD", "OPTIONS"]
5675
cached_methods = ["GET", "HEAD", "OPTIONS"]
5776
target_origin_id = "main"
5877

59-
forwarded_values {
60-
headers = []
61-
query_string = false
62-
63-
cookies {
64-
forward = "none"
65-
}
66-
}
78+
response_headers_policy_id = aws_cloudfront_response_headers_policy.cache_immutable.id
79+
cache_policy_id = data.aws_cloudfront_cache_policy.caching.id
6780

68-
min_ttl = 0
69-
default_ttl = 86400
70-
max_ttl = 31536000
7181
compress = true
7282
viewer_protocol_policy = "redirect-to-https"
83+
84+
lambda_function_association {
85+
event_type = "origin-request"
86+
lambda_arn = module.lambda_doc_router.version_arn
87+
include_body = false
88+
}
7389
}
7490

7591
origin {

0 commit comments

Comments
 (0)