What Is Amazon CloudFront and How Do You Use It? AWS CDN Guide

Sunucun 4 min read

Amazon CloudFront is AWS’s content delivery network (CDN). It caches your website’s images, CSS/JS files, videos and even API responses at edge locations around the world, so visitors get content from the point closest to them. Pages load faster and the load on your origin server drops. In this article we explain how CloudFront works, its core concepts and its cache settings, based on the AWS documentation.

Illustration: AWS

How Does CloudFront Work?

  1. A visitor requests an address such as cdn.yoursite.com/logo.png; DNS routes the request to the nearest CloudFront edge location.
  2. If the file is in that edge location’s cache and hasn’t expired, it is served from there.
  3. If not, CloudFront fetches the file from the origin (an S3 bucket, EC2 server, load balancer or any web server outside AWS), sends it to the visitor and stores it in the cache.
  4. When the file expires, CloudFront asks the origin whether it has changed; if not, the origin returns 304 Not Modified and the cached copy keeps being used.

Core Concepts

  • Distribution: The configuration you create in CloudFront; it gives you a domain such as d111111abcdef8.cloudfront.net. You can attach your own domain with a CNAME and an SSL certificate.
  • Origin: Where the content actually lives: an S3 bucket, EC2, an Elastic Load Balancer or your own server.
  • Cache behavior: Lets you define different caching and routing rules by path pattern (for example /images/*, /api/*).
  • Cache policy: Sets the cache duration (TTL) and which headers, cookies and query strings are included in the cache key.
  • Invalidation: Removes a file from the cache before it expires.

Cache Duration: TTL and Cache-Control

According to the AWS documentation, if you don’t use a cache policy the default TTL is 24 hours. You manage cache duration in two ways: with the Minimum, Default and Maximum TTL values in the cache behavior, or with the Cache-Control header sent by the origin.

Header sent by the originHow long CloudFront caches it (with Minimum TTL = 0)
Cache-Control: max-age=3600The lesser of max-age and Maximum TTL
max-age and s-maxage togetherThe lesser of s-maxage and Maximum TTL; browsers follow max-age
No caching headerThe Default TTL
no-cache, no-store or privateNot cached (if Minimum TTL is greater than 0, cached for the Minimum TTL)

With s-maxage you can set different durations for the CDN and the browser. For example, Cache-Control: max-age=300, s-maxage=86400 caches for 5 minutes in the browser and 1 day in CloudFront. CloudFront also supports the stale-while-revalidate and stale-if-error directives, so it can keep serving the old copy when the origin is temporarily down.

Step by Step: A Static Site with S3 + CloudFront

  1. Upload your files to an S3 bucket. The bucket doesn’t need to be public.
  2. In the CloudFront console, choose Create distribution, select the S3 bucket as the origin and use Origin Access Control (OAC) so that only CloudFront can access the bucket.
  3. Set the viewer protocol to Redirect HTTP to HTTPS and add a certificate from AWS Certificate Manager for your own domain.
  4. Choose a suitable cache policy and create the distribution.
  5. In your domain’s DNS, point a record such as cdn to the CloudFront domain.

To publish an updated file immediately, you can clear it from the cache:

aws cloudfront create-invalidation \
  --distribution-id E1234567890ABC \
  --paths "/css/*" "/index.html"

For frequently changing files, adding a version to the file name (for example style.v42.css) is a more reliable and usually cheaper approach than invalidations.

Security and Extra Features

  • HTTPS with free SSL certificates from AWS Certificate Manager
  • Network-layer DDoS protection with AWS Shield Standard, and optional application-layer rules with AWS WAF
  • Signed URLs and cookies to serve content only to authorized users
  • CloudFront Functions and Lambda@Edge to run small pieces of code at the edge, such as redirects or adding headers

Pricing

Besides the pay-as-you-go model (based on data transfer and number of requests), CloudFront offers flat-rate monthly plans, including a free plan. Because prices and quotas vary by region and plan, check the current details on AWS’s CloudFront pricing page.

For services commonly used as CloudFront origins, see our articles on Amazon S3 and Amazon EC2, and for monitoring, Amazon CloudWatch. If you are looking for a fast origin server to sit behind your CDN, take a look at our VDS server options.

Conclusion

Amazon CloudFront is a CDN that speeds up delivery by serving content from the edge location closest to the visitor and reduces the load on your origin. The right cache behaviors and TTLs, Cache-Control headers and OAC for S3 help you get the best performance and security from CloudFront.

Categories

Latest guides