Skip to content

Commit ed5661e

Browse files
authored
Move to managed cache/origin policies (#101)
* Move to managed cache/origin policies * Give CD more permissions * Pass WAF ACL id as parameter * Pass prod ACL in as parameter
1 parent 6ef85da commit ed5661e

File tree

7 files changed

+47
-32
lines changed

7 files changed

+47
-32
lines changed

infrastructure/bootstrap/github_oidc.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ resource "aws_iam_role_policy" "github_actions_infra" {
177177
Action = "cloudfront:*"
178178
Resource = [
179179
"arn:aws:cloudfront::${data.aws_caller_identity.current.account_id}:distribution/*",
180-
"arn:aws:cloudfront::${data.aws_caller_identity.current.account_id}:origin-access-control/*"
180+
"arn:aws:cloudfront::${data.aws_caller_identity.current.account_id}:origin-access-control/*",
181+
"arn:aws:cloudfront::${data.aws_caller_identity.current.account_id}:cache-policy/*",
182+
"arn:aws:cloudfront::${data.aws_caller_identity.current.account_id}:origin-request-policy/*"
181183
]
182184
},
183185
{

infrastructure/environments/production/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module "web" {
4848

4949
environment = "prod"
5050
name_suffix = ""
51+
cloudfront_web_acl_arn = var.cloudfront_web_acl_arn
5152
domain = var.domain
5253
landing_page_bucket_name = var.landing_page_bucket_name
5354
turnstile_site_key = var.turnstile_site_key

infrastructure/environments/production/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ variable "run_hour_utc" {
5050
default = 5
5151
}
5252

53+
variable "cloudfront_web_acl_arn" {
54+
description = "ARN of the WAF Web ACL created by AWS when enabling CloudFront flat-rate pricing"
55+
type = string
56+
default = "arn:aws:wafv2:us-east-1:087108798373:global/webacl/CreatedByCloudFront-114a103d/52137a35-f978-4dde-b7eb-2a1eaa1c0fd5"
57+
}
58+
5359
variable "lambda_memory_size" {
5460
description = "Memory size for Lambda functions in MB"
5561
type = number

infrastructure/environments/staging/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module "web" {
4747

4848
environment = "staging"
4949
name_suffix = "-staging"
50+
cloudfront_web_acl_arn = var.cloudfront_web_acl_arn
5051
domain = var.domain
5152
landing_page_bucket_name = var.landing_page_bucket_name
5253
turnstile_site_key = var.turnstile_site_key

infrastructure/environments/staging/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ variable "turnstile_site_key" {
3838
default = "0x4AAAAAACTuSJcLuENs4joL"
3939
}
4040

41+
variable "cloudfront_web_acl_arn" {
42+
description = "ARN of the WAF Web ACL created by AWS when enabling CloudFront flat-rate pricing"
43+
type = string
44+
default = "arn:aws:wafv2:us-east-1:087108798373:global/webacl/CreatedByCloudFront-0cf3e787/f5bd2664-3404-4168-ab68-9f9096a7a065"
45+
}
46+
4147
variable "lambda_memory_size" {
4248
description = "Memory size for Lambda functions in MB"
4349
type = number

infrastructure/modules/web/landing_page.tf

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,27 @@ resource "aws_cloudfront_origin_access_control" "landing_page" {
4545
signing_protocol = "sigv4"
4646
}
4747

48+
# CloudFront managed cache/origin request policies
49+
data "aws_cloudfront_cache_policy" "caching_optimized" {
50+
name = "Managed-CachingOptimized"
51+
}
52+
data "aws_cloudfront_cache_policy" "caching_disabled" {
53+
name = "Managed-CachingDisabled"
54+
}
55+
data "aws_cloudfront_origin_request_policy" "all_viewer_except_host_header" {
56+
name = "Managed-AllViewerExceptHostHeader"
57+
}
58+
4859
# CloudFront distribution
60+
# NOTE: This uses flat-rate billing with a free tier. This cannot be configured
61+
# in OpenTofu yet, see https://github.com/hashicorp/terraform-provider-aws/issues/45450
4962
resource "aws_cloudfront_distribution" "landing_page" {
5063
enabled = true
5164
is_ipv6_enabled = true
5265
default_root_object = "index.html"
5366
aliases = [var.domain]
54-
price_class = "PriceClass_100" # US, Canada, Europe only (cheapest)
67+
price_class = "PriceClass_All"
68+
web_acl_id = var.cloudfront_web_acl_arn
5569

5670
# S3 origin for static content
5771
origin {
@@ -80,40 +94,19 @@ resource "aws_cloudfront_distribution" "landing_page" {
8094
target_origin_id = "S3-landing-page"
8195
viewer_protocol_policy = "redirect-to-https"
8296
compress = true
83-
84-
forwarded_values {
85-
query_string = false
86-
cookies {
87-
forward = "none"
88-
}
89-
}
90-
91-
min_ttl = 0
92-
default_ttl = 3600 # 1 hour
93-
max_ttl = 86400 # 24 hours
97+
cache_policy_id = data.aws_cloudfront_cache_policy.caching_optimized.id
9498
}
9599

96100
# API behavior: forward /api/* to API Gateway
97101
ordered_cache_behavior {
98-
path_pattern = "/api/*"
99-
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
100-
cached_methods = ["GET", "HEAD"]
101-
target_origin_id = "APIGateway"
102-
viewer_protocol_policy = "redirect-to-https"
103-
compress = true
104-
105-
# Don't cache API responses
106-
min_ttl = 0
107-
default_ttl = 0
108-
max_ttl = 0
109-
110-
forwarded_values {
111-
query_string = true # Forward query strings (e.g., ?token=...)
112-
headers = ["Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
113-
cookies {
114-
forward = "none"
115-
}
116-
}
102+
path_pattern = "/api/*"
103+
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
104+
cached_methods = ["GET", "HEAD"]
105+
target_origin_id = "APIGateway"
106+
viewer_protocol_policy = "redirect-to-https"
107+
compress = true
108+
cache_policy_id = data.aws_cloudfront_cache_policy.caching_disabled.id
109+
origin_request_policy_id = data.aws_cloudfront_origin_request_policy.all_viewer_except_host_header.id
117110
}
118111

119112
restrictions {

infrastructure/modules/web/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ variable "turnstile_site_key" {
3030
type = string
3131
}
3232

33+
variable "cloudfront_web_acl_arn" {
34+
description = "ARN of the WAF Web ACL for CloudFront, created by AWS when enabling flat-rate pricing. Leave null if not using flat-rate pricing."
35+
type = string
36+
default = null
37+
}
38+
3339
variable "static_files_path" {
3440
description = "Path to the static files directory"
3541
type = string

0 commit comments

Comments
 (0)