@@ -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
4962resource "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 {
0 commit comments