MORTIMERDEV LABS / AWS / CLOUDFRONT FREE · BETA
CloudFront policies,
picked for you.
Choose what you're serving and get the cache, origin request and response headers policies for each path, with the reasons. See how long CloudFront will really keep a response for any Cache-Control, and copy the setup as CDK in TypeScript and Python.
Build your policies
RESULT
Check these
Response headers
How long does CloudFront keep it?
What is a CloudFront policy?
CloudFront is AWS's CDN: it keeps copies of your
responses close to visitors. Each cache behavior (a
path pattern such as /assets/*, plus a default for
everything else) has up to three policies:
- How long, and what varies
- TTLs, and which headers, cookies and query strings are in the cache key
- What the origin gets
- Extra values sent on a cache miss, without splitting the cache
- What viewers get
- Security headers, CORS and custom headers added to responses
How long CloudFront keeps a response
- The origin decides, within limits: CloudFront uses s-maxage, then max-age, then Expires, but never less than the Minimum TTL or more than the Maximum TTL.
- No Cache-Control? Then the Default TTL applies: 24 hours with CachingOptimized.
- A Minimum TTL above 0 wins over no-cache, no-store and private. Use Minimum TTL 0 for anything that mustn't be shared.
Easy to confuse
- Cache key vs origin request: anything in the cache key is also sent to the origin, but not the other way round.
- Browsers don't see your TTLs: they follow the Cache-Control the origin sent. Only s-maxage is just for CloudFront.
- Override: a response headers policy only replaces a header from your origin if origin override is on.
Read more: AWS: cache policiesAWS: origin request policiesAWS: response headers policiesAWS: how long content stays in the cacheCDK: aws-cloudfront
TEST YOURSELF
How well do you know CloudFront policies?
Ten questions on cache keys, TTLs, managed policies and the common traps, with an explanation and a link to the reference after each one. Three ask how long CloudFront keeps a response, answered by the same rules as the TTL checker.
CLOUDFRONT REFERENCE
CloudFront policies, in plain English.
The three policies
Cache policyHow long CloudFront keeps responses (TTLs) and what's in the cache key.
Attached to a cache behavior. Its Minimum, Default and Maximum TTL work together with the origin's Cache-Control and Expires headers. Its cache key settings choose which headers, cookies and query strings make one cached copy different from another.
Cache keyWhat CloudFront uses to find a cached copy: the URL, plus anything the cache policy adds.
Every header, cookie or query string you add to the cache key splits the cache into more copies, which lowers the hit rate. Whatever is in the cache key is also sent to the origin automatically.
Origin request policyWhat CloudFront sends to the origin on a cache miss, without adding it to the cache key.
Use it when the origin needs something (a header, cookies, query strings) that shouldn't split the cache. By default, query strings, headers and cookies from the viewer aren't sent to the origin unless a policy includes them.
Response headers policyHeaders CloudFront adds to, or removes from, responses to viewers: security headers, CORS and custom headers.
Each header has an origin override setting. With override on, CloudFront replaces the header your origin sent; with it off, the origin's header is kept and the policy's is only used when the origin sent none.
TTLs and Cache-Control
Minimum TTLThe shortest time CloudFront keeps a response, whatever the origin says.
If it's above 0, CloudFront keeps responses for at least that long even when the origin sends Cache-Control: no-cache, no-store or private. Browsers still follow the header.
Default TTLUsed only when the origin sends no Cache-Control max-age, s-maxage or Expires.
If the Minimum TTL is above 0, CloudFront uses the greater of the two. With CachingOptimized that's 24 hours, so a page sent without Cache-Control is cached for a day.
Maximum TTLThe longest CloudFront keeps a response, even if the origin asks for longer.
CloudFront caps max-age, s-maxage and Expires at this value. It also caps stale-while-revalidate and stale-if-error: after the Maximum TTL, a stale copy isn't served.
s-maxageCache-Control for shared caches: CloudFront uses it instead of max-age; browsers ignore it.
Cache-Control: max-age=0, s-maxage=300 makes browsers check every time while CloudFront keeps the response for 5 minutes, within the cache policy's Minimum and Maximum TTL.
stale-while-revalidate, stale-if-errorLet CloudFront serve an expired copy while it refetches, or while the origin is failing.
Supported since May 2023. CloudFront serves stale content for up to the directive's value or the Maximum TTL, whichever is less. stale-if-error applies when the origin is unreachable or returns a 5xx error.
Managed policies
CachingOptimizedManaged cache policy: Minimum 1 second, Default 24 hours, Maximum 365 days; nothing in the cache key.
Good for fingerprinted files (app.3f9c2b.js). Poor for HTML: the 1-second minimum ignores no-cache, and without Cache-Control a page is cached for 24 hours.
CachingDisabledManaged cache policy with all three TTLs at 0, which turns caching off.
Every request goes to the origin. Pair it with an origin request policy to send the origin what it needs.
UseOriginCacheControlHeadersManaged cache policy for origins that send Cache-Control: Minimum and Default 0, Maximum 365 days.
Its cache key includes Host, Origin, the X-HTTP-Method-Override family of headers and all cookies, so visitors with different cookies get separate copies. The -QueryStrings variant adds all query strings.
AllViewerExceptHostHeaderManaged origin request policy: every header, cookie and query string except Host.
Meant for API Gateway and Lambda function URL origins, which expect Host to be their own domain name. CloudFront sends the origin's domain name as Host instead.
SecurityHeadersPolicyManaged response headers policy: Referrer-Policy, Strict-Transport-Security, X-Content-Type-Options, X-Frame-Options and X-XSS-Protection.
It has no Content-Security-Policy, HSTS is max-age=31536000 without includeSubDomains, and only X-Content-Type-Options overrides a value your origin sends.
Common traps
Origin Access ControlLets CloudFront read a private S3 bucket, so the bucket doesn't need public access.
CloudFront signs its requests to S3, and the bucket policy allows only that distribution. In CDK, S3BucketOrigin.withOriginAccessControl(bucket) sets up both.
403 for missing filesS3 returns 403 Access Denied, not 404, for a missing key when the caller can't list the bucket.
That's why single-page apps on S3 map 403 as well as 404 to /index.html. The catch: a missing script or image also comes back as your HTML with status 200.
CSP length limitA Content-Security-Policy in a response headers policy can be at most 1,783 characters.
Longer policies have to come from the origin instead. The CSP tool can help shorten one, for example by replacing long host lists with 'self' or a single domain.
A starting point, not your whole distribution.
Domains, certificates, WAF, logging and invalidation on deploy are up
to you. Check the generated CDK with cdk diff before you
deploy it.
Nothing leaves this page.
Everything is worked out in your browser and nothing is saved. We count anonymous page views and button use, never what you enter.
Beta feedback helps shape Labs. Tell Andy what would help
Rules last checked , against CloudFront: how long content stays in the cache, CloudFront: managed cache policies, CloudFront: managed origin request policies, CloudFront: managed response headers policies, CloudFront: response headers policies and CloudFront: forwarding the Authorization header. Re-checked weekly.
