Amazon CloudFront
Last reviewed: 2026-10-27 · Applies to: CloudFront cache policies, origin request policies, Origin Shield, invalidations (path and tag), OpenNext on AWS
Contents
- When to read this
- How to detect
- Default recipe
- Rules and gotchas
- Cache key and TTLs
- Invalidation
- Signed URLs and private content
- OpenNext / SST
- Status headers
- Version notes
- Verify
- Sources
When to read this
When the app is served through CloudFront (directly, via Amplify, SST/OpenNext, Serverless, or IaC). Vendor-neutral concepts: references/layers/cdn-concepts.md.
How to detect
Terraform aws_cloudfront_distribution/aws_cloudfront_cache_policy/aws_cloudfront_origin_request_policy, CDK aws-cloudfront, serverless.yml, sst.config.ts, open-next.config.ts, amplify.yml; response headers x-cache: … from cloudfront, x-amz-cf-pop, x-amz-cf-id, via: … cloudfront.
Default recipe
Full Terraform: assets/snippets/cdn/cloudfront-cache-policy.tf.
{
"Name": "html-anon",
"MinTTL": 0, "DefaultTTL": 300, "MaxTTL": 86400,
"ParametersInCacheKeyAndForwardedToOrigin": {
"EnableAcceptEncodingGzip": true, "EnableAcceptEncodingBrotli": true,
"HeadersConfig": { "HeaderBehavior": "none" },
"CookiesConfig": { "CookieBehavior": "none" },
"QueryStringsConfig": { "QueryStringBehavior": "whitelist",
"QueryStrings": { "Quantity": 2, "Items": ["page", "q"] } }
}
}- One cache behavior per path class: hashed assets (
CachingOptimizedmanaged policy is fine), public HTML (policy above, origin sendss-maxage), private/API paths (CachingDisabledmanaged policy). - Origin sends
Cache-Control: max-age=0, s-maxage=300, stale-while-revalidate=3600, stale-if-error=86400for public HTML: browsers revalidate (max-age=0), CloudFront usess-maxagewithin the policy's min/max. Don't combineno-cachewiths-maxage:no-cacheapplies to shared caches too. CloudFront has no CDN-only header, so this oneCache-Controlserves both. - Enable Origin Shield in the region nearest the origin.
Rules and gotchas
- A cache policy with
MinTTL > 0can override originno-cache,no-storeandprivate. UseMinTTL: 0on any behavior that may serve personalized responses, or route those paths toCachingDisabled. - Cache policy = what's in the key (and forwarded). Origin request policy = what's forwarded without entering the key. Use the origin request policy for cookies/headers the origin needs but that must not split the cache.
- Never put session cookies or
Authorizationin the key "to be safe"; route authenticated paths to a non-caching behavior instead. - With compression enabled in the cache policy, don't also list
Accept-Encodingin the origin request policy. - Forwarding or keying on all query strings increases misses and origin load; allow-list.
- Invalidations are billed beyond 1,000 paths/month per account; a wildcard path or a tag counts as one path. Prefer tags or wildcards over long URL lists.
- Self-hosted frameworks on AWS (OpenNext) still need a CloudFront invalidation after on-demand revalidation; the framework's own cache doesn't purge CloudFront.
Cache key and TTLs
- Managed
CachingOptimized(ID658327ea-f89d-4fab-a63d-7e88639e58f6): min 1 s, default 86,400 s, max 31,536,000 s; no query strings or cookies in the key; normalizedAccept-Encodingonly. Good for hashed assets, wrong for HTML that might be personalized (min TTL 1 s). - Headers listed in an origin request policy are forwarded even when the cache policy's
HeaderBehaviorisnone. - Legacy
ForwardedValues/QueryStringCacheKeyson behaviors are deprecated in favor of policies.
Invalidation
# Path invalidation (wildcard counts as one path)
aws cloudfront create-invalidation --distribution-id "$DIST_ID" --paths "/products/*"
# Tag invalidation (April 2026): opt in with CacheTagConfig naming the origin header that carries tags
aws cloudfront create-invalidation --distribution-id "$DIST_ID" --paths "#product:123"- Tag invalidation: distributions without
CacheTagConfigignore tag headers; the origin sends comma-separated tags in the configured header; at most 50 tags are stored per object; each tag is billed as one path (shares the 1,000 free paths); not in China regions; distribution tenants useCreateInvalidationForDistributionTenant. - Overage price per path: commonly cited as $0.005 (third-party); check the AWS pricing page.
- Concurrency limits are reported inconsistently; batch and queue invalidations from webhooks.
Signed URLs and private content
- Signed URLs for individual files or cookie-less clients; signed cookies for many files (HLS segments) or to keep URLs unchanged. Use trusted key groups.
- URLs containing
Expires,Policy,SignatureorKey-Pair-Idare treated as signed. Whether signature params enter the key wasn't verified; keep them out of the cache policy's query allow-list.
OpenNext / SST
- OpenNext on AWS: S3 incremental cache, DynamoDB tag cache, SQS revalidation queue, plus CloudFront in front. After
revalidateTag/revalidatePath, CloudFront still serves its copy until its TTL or an invalidation: wire an invalidation (or use shorts-maxagewith SWR). - OpenNext AWS docs state support for "all Next.js 15 features"; confirm Next 16 Cache Components support before relying on it.
- See
references/frameworks/nextjs.mdfor the framework side.
Status headers
X-Cache: Hit from cloudfront | Miss from cloudfront | RefreshHit from cloudfront (revalidated with origin) | Error from cloudfront | LimitExceeded from cloudfront. Age on hits; X-Amz-Cf-Pop names the edge. Logs carry OriginShieldHit.
Version notes
- April 2026: tag-based invalidation (
CacheTagConfig). - Cache and origin request policies replaced
ForwardedValues.
ForwardedValueswithCookies: all/QueryString: trueon every behavior.- Invalidating
/*on every deploy instead of hashing assets.
Verify
curl -s -o /dev/null -D - https://d123.cloudfront.net/page | grep -iE 'x-cache|age|cache-control|x-amz-cf-pop'
aws cloudfront get-cache-policy --id "$POLICY_ID" --query 'CachePolicy.CachePolicyConfig.[MinTTL,DefaultTTL,MaxTTL]'
aws cloudfront list-invalidations --distribution-id "$DIST_ID" --max-items 5