Sub topic 9
Serving content through CloudFront
This chapter assumes you know how S3 buckets, EC2 or load balancer origins, and HTTP caching headers work. It also assumes you have read the earlier chapters on internet egress pricing.
Why CloudFront lowers transfer cost
Serving content straight from S3 or EC2 to the internet is billed as data transfer out from the region. CloudFront changes the bill in three ways:
- Transfer from an AWS origin (S3, EC2, an Application Load Balancer) to CloudFront is not charged.
- You pay CloudFront's own per-GB rate for delivery to viewers. That rate is usually lower than regional internet egress and drops at higher volume tiers.
- A cached object is delivered from an edge location without going back to the origin at all.
CloudFront also adds HTTP request charges, so it does not always win for very small objects at low volume. Check the current pricing page for your regions. CloudFront has a monthly free tier for data transfer out.
The cache hit ratio drives savings
Every cache miss makes the edge fetch the object from the origin. The origin fetch is free of transfer charges when the origin is on AWS, but it still costs origin compute and requests. The larger benefit is that every hit is a request your origin never serves. A higher hit ratio means less origin load and less latency.
Raising the hit ratio
Most low hit ratios come from a few causes:
- Short or missing TTLs. Set
Cache-Controlon static assets so edges keep them. - Cache keys that are too wide. If the cache policy includes every query string, header, or cookie, each variation becomes a separate cached object. Include only the values your origin actually uses.
- Frequent invalidations. Use versioned file names such as
app.3f9c1.jsinstead of invalidating paths. - Many edge locations with sparse traffic. Origin Shield adds a regional caching layer in front of the origin, so misses from different edges collapse into fewer origin requests. It has its own per-request charge, so enable it only if origin load justifies it.
This command uploads a static asset with a long cache lifetime:
aws s3 cp ./dist/app.3f9c1.js s3://example-assets-bucket/static/app.3f9c1.js \
--cache-control "public, max-age=31536000, immutable" \
--content-type "application/javascript"Because the file name contains a content hash, a new release uses a new key, so you never need to invalidate the old one.
Other settings that affect cost
- Compression. Enable automatic compression in the cache policy so CloudFront serves gzip or Brotli for compressible types. Fewer bytes leave the edge, which lowers your per-GB charge.
- Price class. Price Class 100 or 200 limits delivery to cheaper edge regions. Viewers elsewhere are served from the nearest included location, which may raise their latency.
- Origin access control. Restrict the S3 bucket so only CloudFront can read it. This stops clients from bypassing the cache and paying direct S3 egress.
Common errors
- Forwarding the
Authorizationheader or all cookies to the origin, which makes almost every request a miss. - Leaving S3 publicly readable, so some traffic skips CloudFront.
- Expecting invalidations to be free. Beyond a monthly allowance of free paths, they are billed per path.
Exercise
A site serves 2 TB of images a month directly from S3 to the internet. The CloudFront cache policy forwards all query strings, and the cache hit ratio is low. Name two changes that would reduce cost.
Show answer
Route the images through CloudFront, because transfer from S3 to CloudFront is free and CloudFront's delivery rate is usually lower than direct S3 egress. Then narrow the cache key to only the query strings the origin needs (and set long Cache-Control TTLs with versioned file names), so more requests are cache hits. Restricting the bucket with origin access control also stops direct S3 downloads.