Skip to Content

Amazon CloudFront

CloudFront as a CDN, its origins, S3 with origin access control, VPC origins and public origins, CloudFront vs S3 replication, geo restriction, invalidations, signed URLs and price classes.

What it is

Amazon CloudFront is AWS's content delivery network (CDN). It improves read performance by caching content at the edge, close to users, so they get a faster experience.

  • Hundreds of points of presence worldwide: edge locations plus larger regional edge caches.
  • DDoS protection, because it's spread worldwide, with AWS Shield included and AWS WAF integration.
  • Requests that do go back to the origin travel over the AWS backbone, not the public internet.

How it works

A CloudFront request: the viewer asks the nearest edge location for /beach.jpg; on a cache hit the edge answers from its cache; on a miss it fetches the file from the origin, an S3 bucket or HTTP server, over the AWS network and caches it for its TTL A CloudFront request: the viewer asks the nearest edge location for /beach.jpg; on a cache hit the edge answers from its cache; on a miss it fetches the file from the origin, an S3 bucket or HTTP server, over the AWS network and caches it for its TTL

Hit: answer from the edge. Miss: fetch from the origin.
  1. A distribution is created with one or more origins (where the real content lives). It gets a name like d111111abcdef8.cloudfront.net, and usually an alias record for a custom domain.
  2. A viewer's request (for example GET /beach.jpg) goes to the edge location with the lowest latency for them.
  3. Cache hit: the edge has the file and answers at once.
  4. Cache miss: the edge forwards the request to the origin, returns the response and caches it for the TTL, so the next viewers nearby get it from the edge.

Origins

  • S3 bucket: for distributing files and caching them at the edge, and even for uploading files to S3 through CloudFront. Secured with origin access control (OAC).
  • VPC origin: for applications in private subnets: a private ALB, NLB or EC2 instance.
  • Custom origin (HTTP): any HTTP server, such as an S3 static website (the website feature must be turned on first), a public ALB, an EC2 instance, API Gateway or a Lambda function URL.
  • Origin groups pair a primary and a secondary origin: if the primary fails or returns certain error codes, CloudFront retries on the secondary (origin failover).

S3 as an origin, with OAC

An S3 origin with origin access control: viewers reach the private bucket only through CloudFront, which signs its requests with OAC; the bucket policy allows only that distribution, so direct requests to S3 are denied An S3 origin with origin access control: viewers reach the private bucket only through CloudFront, which signs its requests with OAC; the bucket policy allows only that distribution, so direct requests to S3 are denied

The bucket stays private; only CloudFront can read it.
  • Origin access control (OAC) makes CloudFront sign its requests to S3.
  • The bucket policy allows s3:GetObject only to the CloudFront service for that distribution. The bucket stays private, with Block Public Access on, and users can't bypass CloudFront.
  • OAC replaces the older origin access identity (OAI): OAC also works with SSE-KMS encrypted objects, all Regions, and PUT/DELETE requests.

ALB or EC2 as an origin

With VPC origins (recommended): CloudFront reaches a private ALB, NLB or EC2 instance in a private subnet through a network interface it creates in the VPC. The application isn't exposed to the internet at all, and CloudFront is the single way in. (The VPC still needs an internet gateway, though CloudFront traffic doesn't use it.)

CloudFront VPC origins: CloudFront delivers content from a private Application Load Balancer, Network Load Balancer or EC2 instance in a private subnet, so the origin needs no public IP address and CloudFront is the single way in CloudFront VPC origins: CloudFront delivers content from a private Application Load Balancer, Network Load Balancer or EC2 instance in a private subnet, so the origin needs no public IP address and CloudFront is the single way in

Private origins with no public address.

Over the public network (the older way):

  • an EC2 instance origin must be public, and its security group must allow the public IP ranges of CloudFront edge locations;
  • an ALB origin must be public and allow the edge IP ranges; the EC2 instances behind it can be private, allowing only the ALB's security group.

CloudFront vs S3 Cross-Region Replication

  • CloudFront: a global edge network; files are cached for a TTL (maybe a day). Great for static content that must be available everywhere.
  • S3 Cross-Region Replication: must be set up for each Region needed; files are updated in near real time; replicas are read-only. Great for dynamic content that must be available at low latency in a few Regions.

Geo restriction

A distribution can restrict who can access it by country:

  • Allowlist: users can access the content only if they're in an approved country.
  • Blocklist: users in banned countries can't access it.

The country comes from a third-party GeoIP database (about 99.8% accurate). Use case: copyright laws that limit where content can be shown.

Cache invalidations

If the origin is updated, CloudFront doesn't know: edges keep serving the old copy until its TTL expires. A CloudFront invalidation forces a refresh, bypassing the TTL:

  • invalidate everything (/*) or a path (/images/*, /index.html);
  • the first 1,000 invalidation paths each month are free; a wildcard path counts as one;
  • paths are case-sensitive.

The alternative is versioned file names (style.v2.css): new names are fetched fresh with no invalidation at all.

Signed URLs and signed cookies

To serve private content only to some users (for example, paying customers), CloudFront can require signed URLs or signed cookies, created by the application with a key from a trusted key group:

  • Signed URL: access to one file, for clients that can't use cookies.
  • Signed cookie: access to many files, such as all the videos of a course, without changing URLs.

These differ from S3 presigned URLs, which grant access to one S3 object directly, using the signer's IAM permissions, with no CDN.

Price classes

CloudFront charges different rates in different parts of the world. A price class leaves out the most expensive edge locations:

  • Price Class All: every location, best performance.
  • Price Class 200: most locations, excluding the most expensive ones.
  • Price Class 100: only the cheapest locations (North America and Europe).

Not in the course slides

Origin groups, signed URLs and cookies, and price classes aren't in this edition of the course slides, but they're common CloudFront exam topics. These sections are written from the AWS documentation. AWS has also added flat-rate pricing plans that bundle CloudFront with WAF, Route 53 DNS and more for a fixed monthly price; that's newer than the exam material.

Real-world examples

  • A news site serves images and pages through CloudFront from a private S3 bucket with OAC. Traffic spikes hit the edge, not the bucket.
  • A video streaming service restricts some films to certain countries with geo restriction, and gives subscribers signed cookies.
  • An internal-facing app moves its ALB into private subnets and serves it through a CloudFront VPC origin, removing every public entry point except CloudFront.

Exam tips

  • "Cache static content globally", "reduce load on the origin", "low latency for users worldwide" → CloudFront.
  • "Only allow access to the S3 content through CloudFront" → origin access control (OAC) and a bucket policy.
  • "Origin in a private subnet without public exposure" → CloudFront VPC origin.
  • "Block users from certain countries" → CloudFront geo restriction.
  • "Users see old content after an update" → invalidation (or versioned file names).
  • "Paid content for many files per user" → signed cookies. "One file" → signed URL.
  • "Reduce CloudFront cost, users only in North America and Europe" → Price Class 100.
  • "High availability between two origins" → origin group (origin failover).
  • "Static content everywhere vs dynamic content in a few Regions" → CloudFront vs S3 CRR.

Common traps

  • "Make the bucket public so CloudFront can read it." Use OAC and keep it private.
  • "OAI is the recommended way." OAI is legacy; OAC is recommended (and needed for SSE-KMS).
  • "CloudFront updates its cache immediately when S3 changes." Only after the TTL, or an invalidation.
  • "S3 presigned URLs and CloudFront signed URLs are the same." Different services, different keys.

Hands-on lab: CloudFront in front of a private bucket

Goal: serve a private S3 bucket through CloudFront with OAC, and see direct access denied.

Costs money

A distribution with a handful of requests costs less than a cent (or nothing on the CloudFront free plan). Deleting it takes a few minutes. Any Region works for the bucket.

Prepare a private bucket

Create a bucket saa-cf-<random> (Block Public Access on) and upload index.html containing <h1>Hello from CloudFront</h1>.

Create the distribution

CloudFront → Create distribution. Origin: choose your bucket (the REST endpoint, not the website endpoint). Origin access: Origin access control settings (recommended) → create a new OAC. Default root object index.html. Skip WAF for this lab. Create. When it shows the bucket policy to copy, copy it.

Allow CloudFront in the bucket policy

In the bucket's Permissions → Bucket policy, paste the policy (it allows cloudfront.amazonaws.com with a condition on your distribution's ARN). Save.

Test

When the distribution is deployed, open https://<distribution>.cloudfront.net: "Hello from CloudFront", over HTTPS. Open the S3 object URL directly: AccessDenied. Update index.html, reload: still the old page until you create an invalidation for /index.html.

Expected result: content served worldwide over HTTPS from a bucket nobody can reach directly.

Clean up: Disable the distribution, wait until it's deployed, Delete it, delete the OAC, then empty and delete the bucket.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt