CloudFront cache and origin access
What an edge cache does
Amazon CloudFront is a content delivery network (CDN). A distribution describes how requests are handled, where content comes from and how caching works. The origin is the source of the response, such as a private S3 bucket or an application behind a load balancer. A cache hit reuses an eligible stored response; a miss may fetch from the origin. A CDN delivers content near viewers; a load balancer distributes requests among backends. They can work together. The fictional bookstore serves public cover images from a private S3 origin.
Viewer -> CloudFront edge -> private S3 originFirst request: MISS (toy)Second matching request: HIT (toy)Cache behaviors and keys
A cache behavior matches a path pattern and chooses an origin and policies; a default behavior handles unmatched paths. Place specific patterns before broader ones when order matters. A cache key identifies which requests can share a stored response; configured query strings, headers and cookies can distinguish variants. For example, include the lang query parameter if it changes a public response. Forwarding data to the origin and including it in the key are different decisions. Never share a personalized response under a key that ignores the viewer identity.
/covers/* -> public-image cache policy/account/* -> application origin, caching disabled/catalog?lang=en != /catalog?lang=fa (lang in key)Freshness and content updates
CloudFront cache policies define minimum, default and maximum TTL. Origin Cache-Control can influence freshness within the relevant policy rules. A positive minimum TTL may cause caching even when origin headers request no caching, so use a caching-disabled policy for private responses and verify origin authorization. For public assets, a new versioned filename avoids waiting for an old object to expire. An invalidation can remove selected cached content before expiry; neither method changes the original object automatically. Do not promise that an update instantly appears everywhere.
/covers/book-v1.png -> old image/covers/book-v2.png -> new imageKeep the S3 origin private
For a regular S3 bucket origin, use Origin Access Control (OAC), with signed origin requests and a bucket policy granting the CloudFront service principal only the needed actions on the object scope, restricted to the intended distribution ARN. Keep S3 Block Public Access enabled. S3 website endpoints are custom origins and cannot use OAC; do not confuse them with regular S3 origins. SSE-KMS adds key permission requirements. OAC protects access to the origin; it does not make the viewer-facing site private. For restricted viewer content, add appropriate viewer authorization.
Anonymous direct S3 GET -> deny (intended)Authorized CloudFront origin GET -> allow (intended)Practice: separate public and private data
Draw a public cover-image path and a private account path. Explain which response can be reused between visitors, where authorization is enforced and why a cache hit cannot prove origin health. Compare cache hit ratio in a synthetic trace of 8 hits and 2 misses: 80% of these ten requests hit the cache. Real delivery costs depend on requests, transfer, region or pricing plan and related resources. Review current prices and cleanup before an optional approved live lab; no distribution or bucket is created here.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CloudFront cache and origin access — knowledge check
Apply the lesson concepts to the fictional bookstore.
What does a cache hit mean in the example?
English and Persian public catalog responses differ by lang query value. What should the key do?
How should private personalized account responses be handled in this beginner design?
Which origin supports OAC for the intended private static assets?
There are 8 hits and 2 misses. What is the sample hit ratio?