Stop 08 · DNS, CDN & Edge ServicesTopics · 5/7

Amazon CloudFront Distributions & Caching

Part of the AWS Roadmap

In short

A user in Guwahati shouldn't wait for every image to travel from a server in Mumbai. CloudFront keeps copies at edge locations close to users, so pages load faster and your origin does less work.

A CloudFront distribution serves content from origins such as S3 buckets, load balancers or any HTTP server through edge locations. Cache behaviours and cache policies control what is cached and for how long, and invalidations remove stale files.

The jaadu, as a neighbourhood shopkeeper, hands a packet straight off a well-stocked shelf to a customer while a delivery van from a faraway dairy waits in the background.

Think of it like this

Local kirana stock

Amul doesn't ship every packet of butter from the dairy in Anand when you ask. The local kirana keeps popular items on its shelf and only reorders when stock runs out or expires. CloudFront edge locations are the kirana shelves, your S3 bucket or server is the dairy, and the TTL is the expiry date.

Watch it happen

Cache miss, then cache hit

User in Guwahati
Edge location
Origin (S3)

GET /img/cake.webp

1/6DNS sent the user to a nearby edge location.

The big ideas

What you need to know

Origins

Where the real content lives: an S3 bucket, an ALB, API Gateway or any public HTTP server. One distribution can have several origins.

Cache behaviours

Path patterns such as /api/* or *.jpg decide which origin and which policies apply. The default behaviour catches everything else.

Cache policy and cache key

A cache policy sets the TTLs and which headers, cookies and query strings form the cache key. Fewer parts in the key means more cache hits.

Invalidations

Removing files from the edges before their TTL ends. For static assets, versioned file names like app.3f9c.js are better than frequent invalidations.

See it in code

Walk through the code

bash
1# Deploy new files to the origin bucket2aws s3 sync ./dist s3://priyabakes-site \3  --delete \4  --cache-control "public, max-age=31536000, immutable" \5  --exclude "index.html"6 7# index.html changes every deploy, so keep it short-lived8aws s3 cp ./dist/index.html s3://priyabakes-site/index.html \9  --cache-control "public, max-age=60"10 11# Clear the old index.html from every edge12aws cloudfront create-invalidation \13  --distribution-id E1ABCDEFGHIJKL \14  --paths "/index.html"

1/3Sync the build to S3. Hashed files get a one-year Cache-Control header.

A typical static-site deploy with AWS CLI v2. Hashed asset files can be cached for a year because their names change whenever their content does.

Save yourself the pain

Common mistakes

Oops

Forwarding all headers, cookies and query strings to the origin for static files.

Do this instead

Every extra value splits the cache. Use the managed CachingOptimized policy for static assets and forward only what the origin needs.

Oops

Running an invalidation of /* after every small deploy.

Do this instead

Use content-hashed file names so new versions have new URLs, and invalidate only entry files like index.html.

Your mission

Watch the cache work

~20 min

Quick check

Your cache hit ratio is very low. Which change is most likely to help?

Go deeper

  1. 1Create a distribution in front of an S3 bucket and compare load times with and without it.
  2. 2Add a cache behaviour for /api/* that does not cache responses.
  3. 3Run an invalidation after updating a file and learn why versioned file names are usually better.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge