AWS Builder Center

Amazon CloudFront Explained: Faster Delivery with a CDN

If your website or files live in one AWS Region, users on the other side of the world wait longer for every request. Amazon CloudFront is AWS’s content delivery network (CDN). It caches your content at edge locations around the world, so users get it from somewhere close to them.

How it works

  1. A user requests a file.
  1. The request goes to the nearest edge location.
  1. If the file is cached there (a cache hit), it’s returned immediately.
  1. If not (a cache miss), CloudFront fetches it from your origin, returns it, and caches a copy for the next user.
An origin can be an S3 bucket, an Application Load Balancer, an EC2 instance, or any HTTP server.

The key concepts

  • Distribution: the CloudFront configuration that ties your origin, cache rules, and domain together.
  • Cache behavior: rules that decide how different URL paths are handled, such as which origin to use, which methods to allow, and how long to cache.
  • TTL: how long an object stays cached before CloudFront checks the origin again.

Securing an S3 origin

A common setup is a private S3 bucket behind CloudFront. Use Origin Access Control (OAC) so that only CloudFront can read from the bucket. Keep Block Public Access on, and let users reach your files only through the distribution.

HTTPS and custom domains

CloudFront serves content over HTTPS by default on its own  *.cloudfront.net  domain. For your own domain, request a certificate in AWS Certificate Manager. For CloudFront, that certificate must be created in the us-east-1 Region. Then point your domain at the distribution using Route 53 or your DNS provider.

Updating cached content

Cached files don’t change until the TTL expires. You have two options when you update a file:
  • Invalidate the path, which tells CloudFront to drop the cached copy. Wide invalidations can add cost.
  • Version your filenames, such as  app.v2.js . It’s often simpler and avoids invalidations altogether.

Private content

To restrict who can view a file, use signed URLs or signed cookies. They give temporary access to specific content without making it public.

Common mistakes

  • Making the S3 bucket public instead of using OAC.
  • Setting a long TTL, then wondering why updates don’t appear.
  • Creating the ACM certificate in the wrong Region.
  • Caching content that should be personalized per user.

Conclusion

CloudFront gives you lower latency and offloads traffic from your origin with very little setup. Start with an S3 origin and OAC, add HTTPS with ACM, and use versioned filenames to keep updates simple. Remember to delete test distributions when you’re done.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article