Amazon CloudFront is the AWS content delivery network (CDN). It keeps copies of your content in locations close to your users, so pages and files load faster and your origin gets fewer requests. It pairs naturally with S3 and Route 53 from the earlier posts.
What it does
- Caches content at edge locations around the world.
- Pulls content from an origin, such as an S3 bucket, a load balancer or any web server.
- Serves content over HTTPS with a certificate from AWS Certificate Manager.
- Lets you keep an S3 bucket private while the site stays public.
Key concepts
| Piece | What it is |
|---|---|
| Distribution | The CloudFront setup that has its own domain name |
| Origin | Where CloudFront gets the content, such as S3 or an ALB |
| Edge location | A site close to users that caches content |
| Cache behavior | Rules for paths, caching and allowed methods |
| TTL | How long an object stays cached |
| Origin access control (OAC) | Lets CloudFront read a private S3 bucket |
| Invalidation | A request to remove cached objects before the TTL ends |
Good to know
- Use OAC to keep the S3 bucket private, and allow only CloudFront in the bucket policy.
- Set a default root object such as
index.html, so the root URL works. - A certificate for CloudFront must be requested in us-east-1 in AWS Certificate Manager.
- Point your domain at the distribution with a Route 53 alias record.
- Use a versioned file name or an invalidation when you change a cached file.
- You can add AWS WAF to a distribution to filter bad requests.
Hands-on: try it in your own account
- Create a private S3 bucket such as
demo-cdn-<your-name>with Block all public access on. - Upload a simple
index.htmlfile. - Open CloudFront and choose Create distribution.
- Pick the S3 bucket as the origin and choose Origin access control. Create the control.
- Set the default root object to
index.htmland create the distribution. - Copy the bucket policy that CloudFront suggests, and add it to the bucket.
- When the distribution is deployed, open its
cloudfront.netdomain name in the browser. - Edit the file, upload it again, and create an invalidation for
/index.htmlto see the change. - Clean up: disable and then delete the distribution, and delete the bucket.
Common mistakes
- Making the S3 bucket public instead of using OAC.
- Forgetting the default root object, then getting an error on the root URL.
- Requesting the certificate in the wrong Region.
- Waiting for the TTL after a change instead of using a new file name or an invalidation.
- Leaving test distributions running.
Key takeaways
- CloudFront caches content close to users and protects your origin.
- Use OAC to keep S3 private.
- Plan for cache changes with versioned names or invalidations.
- Delete practice distributions and buckets when you are done.
Tomorrow I'll start a new AWS topic.
Part of #120DaysOfAWS.
Top comments (0)