Skip to content

AWS CloudFront basics

What is CloudFront?

AWS CloudFront is a Content Delivery Network (CDN) service to accelerates the delivery of content like HTML, CSS, JavaScript, and media files. it also Improves user experience by caching content at edge locations worldwide.

Why Use CloudFront?

We use CloudFront to have:

  • Low latency and high transfer speeds.
  • Scalable and cost-effective for distributing static and dynamic content.
  • Secure content delivery (e.g., HTTPS, OAI, signed URLs).

Key Concepts

  • Distributions: Primary entity for delivering content (Types: Web, RTMP).
  • Origins: The source of content (e.g., S3 bucket, HTTP server).
  • Edge Locations: Physical data centers for caching content closer to users.
  • Cache Behaviors: Rules for how CloudFront handles requests (e.g., caching settings, path patterns).
  • TTL (Time to Live): Duration for caching content at edge locations.
  • HTTPS and SSL/TLS: Secure content delivery with custom SSL certificates.

Working with AWS CloudFront

Prerequisites:

  • AWS CLI installed and configured with appropriate IAM permissions.

Create a CloudFront Distribution

let’s create a CloudFront distribution that serve data from AWS S3 bucket:

Terminal window
aws cloudfront create-distribution --origin-domain-name <origin-domain> --default-root-object index.html
  • Replace <origin-domain> with your S3 bucket or web server domain.

List CloudFront Distributions

to display all existing CloudFront distributions and their details, we use it usually to verify the distribution or find its ID.

Terminal window
aws cloudfront list-distributions

Update a Distribution

to fetch the current configuration of a distribution so you can modify and reapply it

  • Fetch the distribution config:
Terminal window
aws cloudfront get-distribution-config --id <distribution-id>

let’s Modify the configuration file locally, Update the distribution:

Terminal window
aws cloudfront update-distribution --id <distribution-id> --distribution-config file://path-to-config.json

Invalidate Cache (Purge Files)

To remove cached content from all edge locations, forcing CloudFront to fetch updated content from the origin we need to invalidate the cache:

Terminal window
aws cloudfront create-invalidation --distribution-id <distribution-id> --paths "/*"

Delete a Distribution

To delete a Distribution we need to disable the distribution first

Terminal window
aws cloudfront update-distribution --id <distribution-id> --enabled false

Once disabled, Then we can delete it as follow:

Terminal window
aws cloudfront delete-distribution --id <distribution-id>

Conclusion

By understanding AWS CloudFront key concepts and learning basic operations, we can easily manage distributions and optimize content delivery. With these fundamentals, we’re now prepared to explore integrating CloudFront with our applications for content delivery.