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:
| header | meaning |
|---|---|
x-cache: Miss from cloudfront | not in this edge's cache: fetched from the origin |
x-cache: Hit from cloudfront | served from the edge cache |
x-cache: RefreshHit from cloudfront | cached copy expired, revalidated with the origin (304), served from cache |
x-cache: Error from cloudfront | the origin (or CloudFront) returned an error |
age | seconds this copy has been in the cache |
x-amz-cf-pop | the edge location (FRA60-P5: Frankfurt) |
x-amz-cf-id | the request ID: give it to AWS Support, find it in the logs |
via | proof 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.