AWS Cloudfront - A Setup Guide with an Application Load Balancer

Published on: June 20, 2025 • 6 min read

What is a CDN and Why Your Website Needs One

In today’s digital world, users expect websites and apps to load in the blink of an eye. Whether you’re running a personal blog or managing a large-scale e-commerce platform, delivering content quickly and reliably is critical to user experience and SEO performance. This is where a CDN — or Content Delivery Network — comes into play.
A Content Delivery Network (CDN) is a geographically distributed network of servers designed to deliver web content — such as HTML pages, JavaScript files, stylesheets, images, and videos — to users with high availability and performance.
Instead of relying on a single server (which could be halfway around the world from some of your users), a CDN stores cached versions of your content on edge servers located in multiple data centers across the globe.
When a user accesses your site, the CDN intelligently routes their request to the server closest to them geographically, ensuring faster load times and a smoother experience.

Understanding CDN with a YoYo Business Example

Let’s say you’ve started a small business making high-tech yoyos at your home in Sydney, Australia. At first, you ship them locally, and everything works smoothly. But soon, you notice a growing demand from customers in North America.
Now, shipping each yoyo from Sydney to North America takes time and costs more. To speed things up and improve customer satisfaction, you decide to send a big batch of yoyos to your friend who lives in the U.S. So whenever someone from North America places an order, your friend can ship it directly — it’s faster and cheaper.
That’s exactly how a CDN (Content Delivery Network) works.

Instead of sending every request to the original server (your home in Sydney), the CDN stores your static content (like images, videos, and files) on servers located closer to your users (like your friend’s house in North America). When a user visits your website, the CDN serves the content from the nearest location, making the site load much faster.
The content stays cached there until it either expires based on a set time (TTL — Time to Live) or is manually cleared by you.

There are many CDN services available, ranging from free to enterprise-grade solutions. Some of the most popular ones include:

Step-by-Step Guide: Integrating CloudFront with an AWS Load Balancer

Prerequisite

You should have a setup where a server runs a web server (such as Nginx or Apache) to serve files or images, and it’s placed behind a load balancer.
You can also refer to the link [here] .

Make sure to add a cache control header in the config, like:

    add_header Cache-Control "public, max-age=86400";
                    


My Nginx server block looks like:

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
    
        location / {
        root /usr/share/nginx/html;
        add_header Cache-Control "public, max-age=86400";
        }
    }
            


By adding this header, the web server instructs CloudFront and browsers to cache the content for 1 day (86400 seconds).

Why Cache-Control Matters


By default, CloudFront is conservative about caching. Here’s why: So, if your origin sends no Cache-Control header, CloudFront will:

When Cloudfront Caches by Default

Scenario Cached by Default? Notes
Response has Cache-Control: public, max-age=86400 Yes Fully cacheable
Response has Cache-Control: no-cache or private No Explicitly tells CloudFront not to cache
Response has no cache headers Maybe Depends on the CloudFront cache policy TTLs
To learn more about cache control, refer [this] .

Steps to Setup Cloudfront Distribution

  1. In the AWS Console, search for CloudFront, then click on Create a CloudFront distribution.
  2. Provide a name. For simplicity, choose “Single website or app” under the distribution option, then click Next.
  3. Here, CloudFront offers different origin sources — we’ll use Elastic Load Balancer.
  4. Under Origin, browse existing load balancers, select yours, and click Next.
  5. Select Custom origin settings, and choose HTTP only under the protocol (since our target listens on port 80). and click on next You can learn more about origin settings [here] .
  6. cloudfront-custom-origin-settings
    Cloudfront Custom Origin Settings
  7. On the next page, you’ll see security settings where you can enable WAF within CloudFront. Learn more about CloudFront and WAF [here] .
    For now, select “Do not enable security protection” and click Next.
  8. cloudfront-review
    Cloudfront Review
    Click on Create Distribution

    Once created, you’ll see the Last modified state. If it's deploying, wait for a few minutes.

    You can now access the web server using the distribution domain name.

    Check the response headers — CloudFront adds extra headers that indicate cache behavior.
    One important header is x-cache, which shows whether the content is served from an edge location or the origin server.

    For the first request, you'll see:

    Miss from CloudFront – meaning the response is coming from the origin.
    cloudfront-dns-access-cache-header
    Cache headers from Cloudfront
    If you perform a hard reload or open the site in a private/incognito window,

    you'll see the following in the response headers:

    Hit from CloudFront

    This indicates that the content is being served from the edge location, not the origin server.
    cloudfront-dns-access-cache-header-hit
    Cache headers from Cloudfront Edge Location
    This indicates that the content is served from the edge location instead of the origin server.

Adding a Alternate Domain Name

If you want to use your own domain instead of the CloudFront-provided DNS, you need to add Alternate Domain Names (CNAMEs) in the distribution settings.

This requires a valid TLS certificate, which must be issued through AWS — either via ACM or IAM.

You can refer to the official documentation:
CloudFront Alternate Domain Names (CNAMEs)

I have created a fresh certificate in ACM.
Note: You must have DNS access to your domain to complete this process.

  1. Search for Certificate Manager in the AWS Console and click on Request a certificate.
  2. Choose Request a public certificate under Certificate Type, then click Next.
  3. In Fully qualified domain name, enter the DNS name you want to use with your CloudFront distribution.
  4. Leave the remaining settings as default and click Request.
  5. Under the Domains section, a CNAME entry will be provided. Add this to your DNS records for verification.
  6. Create a DNS record of type CNAME using the provided name and value.
  7. Wait a few minutes until the status changes to Success.
Now, in the CloudFront distribution settings, you can add your Alternate Domain Name (CNAME)
cloudfront-alternate-dns
Add Alternate Domain Name
  1. Type the full domain name you want to use.
  2. Click on Next and select the certificate you created in Certificate Manager.
  3. Click Next again and then Add domain.
Now you can see that the domain has been added with the associated TLS certificate.
cloudfront-alternate-dns-added
Add Alternate Domain Name
Add the DNS records as shown under the Route domain to CloudFront option, or add a single CNAME record with your domain name and the CloudFront DNS as the value.

Wait for a few minutes, and you’ll be able to access your server using your custom domain.

Access it multiple times, and you’ll notice the response headers update accordingly.
cloudfront-custom-dns
Custom DNS Web Server

Conclusion

This is a simple example of how we can use CloudFront to serve our content from AWS edge locations.

For larger files or high-traffic applications, this approach significantly improves performance and reduces latency.

It not only enhances user experience but also helps offload traffic from your origin server.

CloudFront also offers features like caching, security integration with WAF, and support for custom domains — making it a powerful choice for content delivery.

Further Reading / References

Cleanup

About the Author

Karan Suneja

Karan Suneja is a DevOps Engineer with a passion for automation, cloud, and all things tech. He believes in continuous learning, sharing knowledge, and simplifying complex problems.

LinkedIn  |  GitHub

Comments