OnCallReadyStart learning

AWS Lesson 38.31 · AWS V: Data, Resilience & the Edge · 17 min read

CloudFront: caching at the edge, OAC, invalidations and TLS

In plain words

CloudFront is a chain of corner shops close to your customers that keep copies of your most popular goods. If the corner shop has the item, the customer gets it at once: a Hit. If not, the shop fetches it from your warehouse, the origin, and keeps a copy for next time: a Miss.

Two things go wrong. Change a product in the warehouse and the corner shops keep selling the old one until their copy expires, unless you recall it with an invalidation. And the warehouse lets in only the chain's own vans (OAC), so a wrong pass means every shop gets a closed door: 403.

CloudFront: caching at the edge, OAC, invalidations and TLS

CloudFront is AWS's CDN: hundreds of edge locations that terminate the user's TLS close to them, serve what they have cached, and fetch the rest from your origin (an S3 bucket, a load balancer, any HTTP server). It makes sites faster and takes load off the origin. It also adds a second place where an old version of a file can live, and a second set of permissions that can answer 403. This lesson reads a distribution in front of a private S3 bucket, follows one object from Miss to Hit to stale, and invalidates it.

Need to know: a distribution has origins and cache behaviors (path patterns -> origin + cache policy + viewer protocol policy). A cache policy decides the TTLs and what is part of the cache key (headers, cookies, query strings). Origin access control (OAC) lets CloudFront sign its requests to a private bucket, whose bucket policy allows the cloudfront.amazonaws.com service principal for that distribution only (AWS:SourceArn). Invalidations remove paths from the edge caches. A custom domain needs an ACM certificate in us-east-1.

The distribution

$ aws cloudfront list-distributions --query 'DistributionList.Items[].[Id,DomainName,Status,Origins.Items[0].DomainName]' --output text
EZKOV3VQR0OCPM	d94282c8675f22.cloudfront.net	Deployed	oncall-lab-assets-111122223333.s3.eu-central-1.amazonaws.com
$ D=$(aws cloudfront list-distributions --query "DistributionList.Items[?Comment=='shop assets'].Id" --output text)
$ aws cloudfront get-distribution --id $D --query 'Distribution.DistributionConfig.{origin: Origins.Items[0].DomainName, oac: Origins.Items[0].OriginAccessControlId, viewer: DefaultCacheBehavior.ViewerProtocolPolicy, cachePolicy: DefaultCacheBehavior.CachePolicyId, root: DefaultRootObject, priceClass: PriceClass}'
{
    "origin": "oncall-lab-assets-111122223333.s3.eu-central-1.amazonaws.com",
    "oac": "EHZPNQAASI101U",
    "viewer": "redirect-to-https",
    "cachePolicy": "658327ea-f89d-4fab-a63d-7e88639e58f6",
    "root": "index.html",
    "priceClass": "PriceClass_All"
}
$ aws cloudfront list-cache-policies --type managed --query "CachePolicyList.Items[?CachePolicy.Id=='658327ea-f89d-4fab-a63d-7e88639e58f6'].CachePolicy.CachePolicyConfig.[Name,DefaultTTL,MaxTTL]" --output text
Managed-CachingOptimized	86400	31536000

One S3 origin, reached through an OAC; HTTP redirected to HTTPS; the managed CachingOptimized policy (default TTL one day, unless the object's Cache-Control says otherwise). The bucket itself stays private:

$ aws s3api get-bucket-policy --bucket oncall-lab-assets-111122223333 --query Policy --output text | jq '.Statement[0] | {Principal, Action, Condition}'
{
  "Principal": {
    "Service": "cloudfront.amazonaws.com"
  },
  "Action": "s3:GetObject",
  "Condition": {
    "StringEquals": {
      "AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/EZKOV3VQR0OCPM"
    }
  }
}
$ curl -s -o /dev/null -w '%{http_code}\n' https://oncall-lab-assets-111122223333.s3.eu-central-1.amazonaws.com/index.html
403

The policy grants s3:GetObject to CloudFront only when the request comes from this distribution (AWS:SourceArn). Direct access to the bucket is refused, so users cannot bypass the CDN (and its WAF). OAC replaced the older OAI (origin access identity): OAC signs with SigV4, supports SSE-KMS buckets and all Regions, and is what new setups use.

Miss, Hit, and the headers that tell you

$ DOM=$(aws cloudfront get-distribution --id $D --query Distribution.DomainName --output text)
$ curl -sI http://$DOM/ | head -3
HTTP/1.1 301 Moved Permanently
Server: CloudFront
Date: Tue, 22 Sep 2026 20:00:04 GMT
$ curl -sI https://$DOM/index.html | grep -iE '^(HTTP|x-cache|age|via|x-amz-cf-pop|x-amz-cf-id|cache-control|content-type)'
HTTP/2 200
content-type: text/html
x-cache: Miss from cloudfront
via: 1.1 f1b8566a991d362107c23eaf56180751.cloudfront.net (CloudFront)
x-amz-cf-pop: FRA60-P5
x-amz-cf-id: 7WAOH5HJ3ncEh5s5CfYatTEPtP75xjvHtABXa0O6eVrbOluqZ3svocTO==
$ curl -sI https://$DOM/index.html | grep -iE '^(x-cache|age)'
x-cache: Hit from cloudfront
age: 0

How to read a CloudFront response:

headermeaning
x-cache: Miss from cloudfrontnot in this edge's cache: fetched from the origin
x-cache: Hit from cloudfrontserved from the edge cache
x-cache: RefreshHit from cloudfrontcached copy expired, revalidated with the origin (304), served from cache
x-cache: Error from cloudfrontthe origin (or CloudFront) returned an error
ageseconds this copy has been in the cache
x-amz-cf-popthe edge location (FRA60-P5: Frankfurt)
x-amz-cf-idthe request ID: give it to AWS Support, find it in the logs
viaproof the response went through CloudFront

The stale page, and the invalidation

A deploy uploads a new index.html to the bucket:

$ printf '<h1>Shop v2</h1>\n' > /tmp/index.html && aws s3api put-object --bucket oncall-lab-assets-111122223333 --key index.html --body /tmp/index.html --content-type text/html --query ETag --output text
"47d924bc0ac26286492e826205223ab6"
$ curl -s https://$DOM/index.html
<h1>Shop v1</h1>
$ aws cloudfront create-invalidation --distribution-id $D --paths '/index.html' --query 'Invalidation.[Id,Status]' --output text
IDCZWEVO3ZWNDX	InProgress
$ aws cloudfront wait invalidation-completed --distribution-id $D --id $(aws cloudfront list-invalidations --distribution-id $D --query 'InvalidationList.Items[0].Id' --output text)
$ curl -s https://$DOM/index.html
<h1>Shop v2</h1>
$ curl -sI https://$DOM/index.html | grep -i '^x-cache'
x-cache: Hit from cloudfront

The origin changed, and the edge kept serving v1 until its TTL ran out, up to a day with this policy. An invalidation removes the paths from every edge (/* for everything). The first 1,000 paths a month are free, then they are billed per path. The better fix is in the build: versioned file names (app.3f9a2c.js, never invalidated, cached for a year) plus a short TTL or Cache-Control: no-cache on index.html, the only file that points at them.

TLS and custom domains

$ aws cloudfront get-distribution-config --id $D --query 'DistributionConfig.ViewerCertificate'
{
    "CloudFrontDefaultCertificate": true,
    "SSLSupportMethod": "vip",
    "MinimumProtocolVersion": "TLSv1",
    "CertificateSource": "cloudfront"
}
$ aws acm list-certificates --region eu-central-1 --query 'CertificateSummaryList[].[DomainName,CertificateArn]' --output text
cdn.shop.oncall-lab.example	arn:aws:acm:eu-central-1:111122223333:certificate/70674259-975f-4724-a6b0-01c6323287a7
$ aws cloudfront get-distribution-config --id $D --query DistributionConfig > /tmp/cfg.json && jq '.CallerReference = "try-alias" | .Aliases = {"Quantity": 1, "Items": ["cdn.shop.oncall-lab.example"]} | .ViewerCertificate = {"ACMCertificateArn": "'$(aws acm list-certificates --region eu-central-1 --query 'CertificateSummaryList[0].CertificateArn' --output text)'", "SSLSupportMethod": "sni-only", "MinimumProtocolVersion": "TLSv1.2_2021"}' /tmp/cfg.json > /tmp/alias.json && aws cloudfront create-distribution --distribution-config file:///tmp/alias.json
aws: [ERROR]: An error occurred (InvalidViewerCertificate) when calling the CreateDistribution2020_05_31 operation: The specified SSL certificate doesn't exist, isn't in us-east-1 region, isn't valid, or doesn't include a valid certificate chain.

The default *.cloudfront.net certificate covers the distribution's own name. A custom name (cdn.shop.oncall-lab.example, an alternate domain name) needs an ACM certificate in us-east-1, whatever Region the origin is in: CloudFront is a global service and reads its certificates there. The one in eu-central-1 (made for the load balancer) is refused. Then the DNS name is an alias record in Route 53 to the distribution. Use sni-only, TLSv1.2_2021 or newer, and viewer protocol policy redirect-to-https. The origin connection has its own protocol setting: HTTPS to an ALB origin needs a certificate there too (any Region, the ALB's own).

What else a distribution does

  • Several origins and behaviors: /api/* to the ALB with caching disabled (managed CachingDisabled policy) and all headers forwarded through an origin request policy; /* to S3. Dynamic content still benefits: TLS ends close to the user and reused connections run over AWS's network.
  • Origin groups: a primary and a secondary origin; on 5xx or timeouts CloudFront retries the secondary (an S3 bucket replicated to another Region, for example).
  • Restricting content: signed URLs / signed cookies (key groups) for paid content, geo restriction by country, WAF web ACLs (next lesson).
  • Edge code: CloudFront Functions (tiny, cheap: rewrites, redirects, headers) and Lambda@Edge (full Lambda at the edge).
  • Cost and reach: price classes limit which edges serve (and what you pay); Origin Shield adds a regional cache layer that reduces origin load.
  • Logs: standard logs to S3 (or CloudWatch Logs), real-time logs to Kinesis. The metrics are in us-east-1: Requests, 4xxErrorRate, 5xxErrorRate, CacheHitRate (additional metrics).

On the exam (SOA-C03 5.2 and 5.3, SAA-C03 3.4, CLF-C02 3.2): "serve a private S3 bucket only through CloudFront" = OAC + a bucket policy with AWS:SourceArn; "users see old content after a deploy" = invalidation (or versioned object names); "custom domain over HTTPS" = an ACM certificate in us-east-1; "improve the cache hit ratio" = remove unneeded headers, cookies and query strings from the cache key; "restrict by country" = geo restriction; "paid content" = signed URLs/cookies. Global Accelerator is the other edge service: static anycast IPs and fast failover for TCP/UDP, no caching.

In an interview: "After moving the bucket behind CloudFront with OAC, everything returns 403. Where do you look?" - "First what returns the 403: x-cache: Error from cloudfront with an S3 AccessDenied body means S3 refused. Then the bucket policy: is there a statement for cloudfront.amazonaws.com with the right distribution ARN in AWS:SourceArn? Is the origin configured with the OAC (not an old OAI)? Is the object encrypted with a KMS key whose key policy does not let CloudFront decrypt? And does the default root object exist (a missing key is also 403 when the caller cannot list the bucket)?"

You can now: read a distribution's origins, behaviors, cache policy and OAC, explain how OAC and the bucket policy keep the bucket private, read x-cache, age and the other CloudFront headers, handle stale content with invalidations and versioned names, and attach a custom domain with a us-east-1 certificate.

Why it helps

A CDN makes sites faster and takes load off the origin, but it adds a second place where an old file can live and a second set of permissions that can refuse a request. "Users still see yesterday's page" and "everything returns 403 after the migration" are two of the most common edge incidents.

Reading x-cache and age, knowing what the bucket policy must say for OAC, and knowing that a custom domain needs a certificate in us-east-1 make those incidents quick to solve.

Commands in this lesson

aws curl printf

FAQ

What do the x-cache and age headers tell me?

x-cache says what the edge did: Miss (fetched from the origin), Hit (served from cache), RefreshHit (expired copy revalidated with a 304) or Error. age is how many seconds that copy has been cached. x-amz-cf-pop names the edge location, and x-amz-cf-id is the request ID to find in the logs or give to AWS Support.

Why do users see the old page after a deploy?

The edge serves its cached copy until the TTL runs out; with the managed CachingOptimized policy the default is a day, unless the object's Cache-Control says otherwise. An invalidation removes the path from every edge. The better fix is versioned asset names cached for a long time, plus a short TTL on index.html.

Do invalidations cost money?

The first 1,000 paths a month are free, then each path is billed; /* counts as one path but empties the whole cache, which sends a wave of requests to the origin. Invalidating every file on every deploy is a sign the build should produce versioned file names instead.

What is the difference between OAC and OAI?

Both let CloudFront read a private bucket. OAC, origin access control, is the newer one: it signs requests with SigV4, works with SSE-KMS buckets and in all Regions, and is what new setups use. The bucket policy allows the cloudfront.amazonaws.com service principal, with a condition on AWS:SourceArn so only that distribution gets in.

Why is my certificate refused for a custom domain?

CloudFront is a global service and reads certificates from ACM in us-east-1, whatever Region the origin is in. A certificate made in eu-central-1 for a load balancer cannot be used. Request one in us-east-1, add the alternate domain name, use sni-only and TLSv1.2_2021 or newer, then point an alias record at the distribution.

In an interview Mid

After moving a bucket behind CloudFront with OAC, everything returns 403. Where do you look?

First what returns the 403: x-cache: Error from cloudfront with an S3 AccessDenied body means S3 refused. Then the bucket policy: is there a statement for cloudfront.amazonaws.com with the right distribution ARN in AWS:SourceArn? Is the origin configured with the OAC rather than an old OAI? Is the object encrypted with a KMS key whose key policy does not let CloudFront decrypt? And does the default root object exist, since a missing key is also 403 when the caller cannot list the bucket?

Also asked: How do you stop users from bypassing CloudFront and reading the S3 bucket directly? · How would you improve a CloudFront cache hit ratio? · What is the difference between CloudFront and Global Accelerator?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.