Amazon CloudFront :- Content Delivery Network (CDN)

Amazon CloudFront :- Content Delivery Network (CDN)

Today we will explore Amazon CloudFront a content delivery network (CDN) service provided by AWS. CloudFront accelerates the delivery of your websites, APIs, video content and other web assets to users globally. In this blog post we'll cover the basics of CloudFront, walk through setting up a CloudFront distribution and discuss strategies to optimize content delivery for both performance and cost.

What is Amazon CloudFront?

Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications and APIs to customers globally with low latency and high transfer speeds. CloudFront integrates seamlessly with other AWS services and can be configured to work with origins such as Amazon S3, Amazon EC2 and your own HTTP servers.

Key Features of Amazon CloudFront

  1. Global Network :- Over 200 edge locations across the globe.

  2. Low Latency and High Transfer Speed :- Efficient content delivery by caching content at edge locations.

  3. Security :- Integrates with AWS Shield for DDoS protection and AWS WAF for application-level security.

  4. Seamless Integration :- Works well with AWS services like S3, EC2 and Lambda@Edge for serverless computing.

  5. Customizable :- Flexible caching options, support for multiple origin servers and customizable error responses.

Setting Up an Amazon CloudFront Distribution

Let's set up a CloudFront distribution to deliver content from an Amazon S3 bucket. We'll go through the steps to create a distribution, configure caching behavior and optimize for performance.

Step 1 :- Sign In to the AWS Management Console

  1. Log in to the AWS Management Console.

  2. Navigate to the CloudFront Dashboard :-

    • Search for "CloudFront" in the AWS services search bar and select "CloudFront".

Step 2 :- Create an S3 Bucket

Before creating a CloudFront distribution we need an origin for our content. We'll use an S3 bucket.

  1. Navigate to the S3 Dashboard :-

    • Search for "S3" in the AWS services search bar and select "S3".
  2. Create a New Bucket :-

    • Click "Create bucket".

    • Enter a unique bucket name (e.g. my-cloudfront-demo-bucket).

    • Choose a region and configure settings as needed.

    • Click "Create bucket".

  3. Upload Content to the Bucket :-

    • Select your bucket from the list.

    • Click "Upload" and add files you want to distribute (e.g. images, videos, HTML files).

Step 3 :- Create a CloudFront Distribution

  1. Navigate to the CloudFront Dashboard :-

    • In the AWS Management Console go to the CloudFront service.
  2. Create a New Distribution :-

    • Click "Create Distribution".

    • Choose "Web" for the delivery method.

  3. Configure Origin Settings :-

    • Origin Domain Name :- Select your S3 bucket (e.g.

    • Origin Path :- Leave blank unless you want to restrict to a specific directory within the bucket.

    • Origin ID :- Auto-filled or enter a custom ID.

    • Restrict Bucket Access :- Set to "Yes" for security; this will create an origin access identity (OAI).

  4. Configure Default Cache Behavior Settings :-

    • Viewer Protocol Policy :- Choose "Redirect HTTP to HTTPS" for better security.

    • Allowed HTTP Methods :- Select appropriate methods, typically "GET, HEAD" for static content.

    • Cache Based on Selected Request Headers :- Choose "None (Improves Caching)" for better performance.

  5. Configure Distribution Settings :-

    • Price Class :- Choose "Use All Edge Locations" for best performance or a lower price class for cost savings.

    • Alternate Domain Names (CNAMEs) :- Add custom domain names if needed.

    • SSL Certificate :- Choose "Default CloudFront Certificate" or upload a custom certificate.

    • Default Root Object :- Enter index.html if you have a default homepage.

  6. Review and Create :-

    • Review your settings and click "Create Distribution".

Step 4 :- Verify and Test Your Distribution

  1. Monitor Distribution Status :-

    • The status will change from "In Progress" to "Deployed" once ready.
  2. Test Content Delivery :-

Optimizing Content Delivery

Step 5 :- Configure Cache Behaviors

  1. Navigate to Cache Behaviors :-

    • In the CloudFront dashboard select your distribution.

    • Click the "Behaviors" tab.

  2. Create Additional Behaviors :-

    • Click "Create Behavior" to add new caching rules.

    • Path Pattern :- Define the pattern (e.g. *.jpg for images).

    • Viewer Protocol Policy :- Choose appropriate policy.

    • Allowed HTTP Methods :- Select as needed.

    • Cache Based on Selected Request Headers :- Adjust to control caching behavior.

  3. Optimize TTL (Time to Live) Settings :-

    • Minimum TTL :- Set based on how frequently your content changes.

    • Maximum TTL :- Higher values for static content.

    • Default TTL :- Balance between performance and freshness.

Step 6 :- Enable Compression

  1. Navigate to Cache Behavior Settings :-

    • Select a cache behavior and click "Edit".
  2. Enable Compression :-

    • Check the box for "Compress Objects Automatically".

    • This reduces the size of files sent to viewers improving load times.

Step 7 :- Implement Lambda@Edge for Custom Logic

Lambda@Edge allows you to run code closer to your users enabling custom logic for your CloudFront requests and responses.

  1. Create a Lambda Function :-

    • Navigate to the Lambda service in the AWS Management Console.

    • Click "Create function" and configure a new function.

  2. Deploy Lambda@Edge :-

    • In your Lambda function click "Actions" and select "Deploy to Lambda@Edge".

    • Configure the trigger for your CloudFront distribution.

  3. Example Use Case : URL Redirection :-

    • Write a Lambda function to handle URL redirection based on specific conditions (e.g. user location).
import json

def lambda_handler(event, context):
    request = event['Records'][0]['cf']['request']
    headers = request['headers']

    # Redirect based on user-agent
    if 'user-agent' in headers and 'mobile' in headers['user-agent'][0]['value']:
        return {
            'status': '302',
            'statusDescription': 'Found',
            'headers': {
                'location': [{
                    'key': 'Location',
                    'value': '' + request['uri']

    return request

Step 8 :- Monitor and Analyze Performance

  1. CloudFront Metrics and Logs :-

    • Use CloudFront’s built-in metrics and logs for monitoring.

    • Navigate to the "Monitoring" tab in your CloudFront distribution to view real-time data.

  2. Amazon CloudWatch :-

    • Set up CloudWatch Alarms to get notified about performance issues.

    • Create dashboards to visualize CloudFront performance metrics.

  3. AWS CloudTrail :-

    • Enable CloudTrail to log and monitor API calls and actions taken on your CloudFront distribution.

Best Practices for Using Amazon CloudFront


  1. Use HTTPS :-

    • Always serve your content over HTTPS to ensure data integrity and security.

    • Configure CloudFront to use SSL/TLS certificates for your custom domains.

  2. Restrict Access :-

    • Use signed URLs and signed cookies to restrict access to premium content.

    • Implement AWS WAF to protect against common web exploits and bots.

  3. Enable AWS Shield :-

    • Protect your distribution from DDoS attacks by enabling AWS Shield Standard or Advanced.

Cost Optimization

  1. Optimize Cache TTLs :-

    • Adjust TTL settings to balance between performance and cost.

    • Cache longer for static content to reduce origin requests.

  2. Use Regional Edge Caches :-

    • Leverage Regional Edge Caches to improve cache hit ratios and reduce latency.
  3. Monitor Usage :-

    • Regularly monitor your usage and costs using AWS Cost Explorer.

    • Identify and address any unexpected spikes in usage.


  1. Use Multiple Origins :-

    • Distribute content across multiple origins for better availability and performance.

    • Configure origin failover to automatically switch to a backup origin if the primary fails.

  2. Enable HTTP/2 :-

    • HTTP/2 provides significant performance improvements over HTTP/1.1 including multiplexing and header compression.
  3. Leverage Edge Functions :-

    • Use Lambda@Edge to customize content delivery based on user attributes and requests.


Amazon CloudFront is a powerful and flexible CDN that can significantly improve the performance and security of your web applications. By following the steps outlined in this blog post you can set up and optimize a CloudFront distribution to deliver your content efficiently and cost-effectively. We covered the basics of CloudFront, setting up a distribution, configuring caching behaviors, enabling compression and using Lambda@Edge for custom logic.

Stay tuned for more insights in our upcoming blog posts.

Let's connect and grow on LinkedIn :Click Here

Let's connect and grow on Twitter :Click Here

Happy Cloud Computing!!!

Happy Reading!!!

Sudha Yadav