Introduction

Overview

Amazon CloudFront is the AWS content delivery network (CDN). It speeds up delivery of static and dynamic web content, such as HTML, CSS, JavaScript, images, video, and API responses, by serving it from a worldwide network of edge locations (points of presence) that sit close to your viewers. Requests travel over the AWS backbone network instead of the public internet, so viewers get lower latency and higher transfer rates, and your origin handles fewer requests.

How CloudFront delivers content

  1. You store the original version of your content on an origin, such as an Amazon S3 bucket, an Application Load Balancer, or a web server on an Amazon EC2 instance.
  2. You create a distribution that tells CloudFront which origins to use and how to cache and deliver content. CloudFront gives the distribution a domain name such as d111111abcdef8.cloudfront.net, and you can add your own domain name.
  3. CloudFront sends the distribution configuration (not your content) to its edge locations.
  4. A viewer request goes to the edge location with the lowest latency. On a cache hit, CloudFront returns the object from the edge cache right away.
  5. On a cache miss, CloudFront checks its regional edge caches, a larger mid-tier cache, and then requests the object from the origin if needed. CloudFront then caches the object for later requests. By default, objects stay in the cache for 24 hours unless your cache policy or origin Cache-Control headers set a different time.

Best practices

Security

  • Keep S3 origins private with origin access control (OAC). AWS recommends OAC over the older origin access identity (OAI). OAC supports all AWS Regions, SSE-KMS encrypted objects, and PUT and DELETE requests. OAC does not work with S3 website endpoints, which must be set up as custom origins.
  • Keep EC2 and load balancer origins off the public internet with VPC origins. VPC origins let CloudFront reach Application Load Balancers, Network Load Balancers, and EC2 instances in private subnets, so CloudFront is the only entry point to your application.
  • Use HTTPS end to end. Set the viewer protocol policy to redirect HTTP to HTTPS, use a TLS certificate from AWS Certificate Manager (ACM) for your custom domain, and use HTTPS between CloudFront and your origin when the origin supports it.
  • Protect the distribution with AWS WAF. Attach a web ACL to block common web exploits, bots, and request floods at the edge. CloudFront also includes AWS Shield Standard DDoS protection at no extra cost.
  • Add security headers such as Strict-Transport-Security and X-Content-Type-Options with a response headers policy.

Performance and cost

  • Use cache policies and managed policies. Include only the headers, cookies, and query strings your origin needs in the cache key. Fewer values in the cache key give a higher cache hit ratio. Use an origin request policy to forward extra values to the origin without adding them to the cache key.
  • Turn on compression. Enable “Compress objects automatically” on the cache behavior and enable both Gzip and Brotli in the cache policy. Compressed files can be less than a quarter of their original size, and they cost less to deliver. Brotli requires a cache policy.
  • Set TTLs on purpose. Use long TTLs for versioned static files, and short TTLs or no caching for dynamic content. Prefer versioned file names over frequent invalidations.
  • Consider Origin Shield. Origin Shield adds a caching layer in front of your origin, which raises the cache hit ratio and combines requests for the same object so fewer reach the origin.
  • Use origin failover for content that must stay available when the primary origin fails.
  • Pick the right edge function. Use CloudFront Functions for lightweight viewer request and response logic, and Lambda@Edge only when you need what it adds.
  • Review pricing options. Data transfer from AWS origins to CloudFront is free. Compare pay-as-you-go pricing with the flat-rate pricing plans (Free, Pro, Business, and Premium), which bundle CloudFront, AWS WAF, DDoS protection, Route 53 DNS, and other features for a monthly price with no overage charges.

Operations

  • Turn on access logs and query them with Amazon Athena for traffic analysis, troubleshooting, and audits. Use real-time access logs when you need to monitor within seconds.
  • Create CloudWatch alarms on metrics such as the 4xx and 5xx error rates.
  • Review your configuration regularly. AWS Config provides a “Security Best Practices for Amazon CloudFront” conformance pack that checks distributions against common security rules. s