Overview
CloudFront is a Content Delivery Network (CDN). Anytime you see CDN on the exam, think CloudFront. It improves read performance by caching the content of your website at different edge locations. Because content is cached around the world, users everywhere get lower latency, which improves the user experience.
CloudFront is made of 216 points of presence globally, which correspond to the AWS edge locations around the world. AWS keeps adding locations.
Because content is distributed globally, CloudFront gives DDoS protection. A DDoS attack hits all your servers at the same time. Since your application is worldwide, you are protected, also through AWS Shield and Web Application Firewall (WAF), covered later in the security section.
How CloudFront Works
- Edge locations sit around the world and connect to your origin (an S3 bucket or an HTTP server).
- A client sends an HTTP request to an edge location.
- The edge location checks its cache.
- Cache miss - it goes to the origin to get the result.
- It stores the result in its local cache.
- Cache hit - the next client requesting the same content from the same edge location is served without going to the origin.
Example: an S3 bucket and website in Australia, and a user in America. The user requests content from an American edge location using CloudFront, which fetches it from Australia. A second US user requesting the same content is served directly from the edge, not from Australia. A user in China talks to a Chinese point of presence, which is redirected to the S3 bucket, and the content is then cached at that edge.
CloudFront edge to origin flow
Origins
| Origin | Details |
|---|---|
| S3 bucket | Distribute files and cache them at the edge. Secured with Origin Access Control (OAC), which replaces the older Origin Access Identity (OAI). CloudFront can also be used to upload files to S3 (ingress). |
| Custom origin (HTTP) | Application Load Balancer, EC2 instance, an S3 website (first enable the bucket as a static S3 website), or any HTTP backend |
With S3 as the origin, the edge location (e.g. Los Angeles, or one serving users in Sao Paulo, Brazil) gets content from the S3 bucket through a private network. The bucket is secured with OAC and by modifying the S3 bucket policy. Content of a bucket in one region is thereby distributed around the world through the points of presence.
S3 origin secured with OAC
CloudFront vs S3 Cross-Region Replication
| Feature | CloudFront | S3 Cross-Region Replication |
|---|---|---|
| Network | Global Edge network, ~216 points of presence | Must be set up for each region you want, not every region in the world |
| Freshness | Files cached in each edge location, maybe for a day | Files updated in near real-time, no caching |
| Access | Cached content | Read only |
| Best for | Static content available everywhere | Dynamic content that changes often and needs low latency in a few regions |
CloudFront is a CDN that caches content around the world. S3 Cross-Region Replication replicates an entire bucket into another region.
Static content that must be available worldwide with low latency -> CloudFront. Dynamic content needed at low latency in only a few regions -> S3 Cross-Region Replication.
Hands-On Walkthrough
Create the bucket and files
- Create an S3 bucket (e.g.
demo-cloudFront-stephane-V4), leave defaults, click Create bucket. - Upload three files:
beach,coffee,index.html. - Open
index.html. The Object URL gives Access Denied (object not public). The Open button works because it generates a pre-signed URL (see S3 Pre-signed URLs). The page shows "I love coffee" and "Hello World" but the image is missing because the image is not public.
Create the distribution
- Open the CloudFront console. It is a global service with no region selection.
- Choose the origin domain: your S3 buckets, load balancer, or any domain name including custom HTTP origins.
- Origin access options: public (bucket must be public, not our case), Origin access control settings (OAC) (recommended), or OAI (the previous way).
- Click Create control setting (a default name is given) and Create.
- Leave the bucket policy for now. At this point the bucket policy is empty, so CloudFront cannot read the bucket yet.
- For WAF, disable the security protections (not needed for the demo).
- Set the default root object to
index.html. - Click Create distribution. Creation takes some time.
Update the bucket policy
- A banner says the S3 bucket policy needs to be updated. Click Copy policy, then use the shortcut to the bucket's policy settings.
- Click Edit and paste the policy.
- If you lose the policy, go to Origins -> Edit on the distribution and copy the policy statement from there.
- The AWS-generated policy allows the CloudFront service principal to do
s3:GetObjecton any file in the bucket, with a condition that the source is the distribution you created. No other changes are needed.
Test
- Wait until the distribution status is Deployed.
- Copy the distribution domain name into a new tab. You see "I love coffee", "Hello World" and the image.
/coffee.jpegand/beach.jpegalso work. - Refresh. Content is now served from the CloudFront cache, not S3, and loads much quicker. One request goes to S3 through CloudFront and the rest is cached on the distribution.
- In the left menu, Origin access lists the origin access controls. The one created is active and assigned to this distribution.
Questions & Answers
Q1: What is CloudFront?
Answer:
A CDN that caches content at edge locations (216 points of presence) to lower latency for global users and give DDoS protection (with Shield and WAF).
Q2: How do you make sure only CloudFront can access your S3 bucket?
Answer:
Use Origin Access Control (OAC) (replaces OAI) and update the S3 bucket policy to allow the distribution's s3:GetObject.
Q3: CloudFront or S3 Cross-Region Replication?
Answer:
CloudFront for cached static content available everywhere. Cross-Region Replication for near real-time, read-only, uncached copies of dynamic content in a few chosen regions.
Q4: Which origins does CloudFront support?
Answer:
S3 buckets (with OAC), and custom HTTP origins such as ALB, EC2 instances, S3 websites, or any HTTP backend.
Q5: Is CloudFront regional?
Answer:
No. It is a global service with no region selection in the console.
Check yourself
Quiz
Flashcards
Click the card (or focus it), then use ← → to move and Space to flip.