cloudflare cdn with amazon s3 website

Configure Cloudflare CDN to serve files from an Amazon S3 bucket website

We assume you have already setup a S3 bucket website the right way using a custom domain / sub-domain. This post continues from that point to guide you to use Cloudflare CDN with your S3 bucket website.

Benefits of Cloudflare CDN

  • Caching to reduce the load on my webserver
  • Geographic Distribution for speedy delivery regardless of where you are
  • DDoS Protection from sudden spikes in traffic
  • Compression of files for faster delivery for user on slow internet
  • Secure padlocked status in the web browser
  • No cost or low cost based on pricing plan selected

Change to Cloudflare name servers

To start using the Cloudflare CDN you will need to start managing your domain name's DNS via Cloudflare's name servers.

When you signup with Cloudflare and add your domain name, Cloudflare will import all existing DNS records for your domain from your existing name server (DNS management service provider). This include the DNS record you have added for your S3 bucket website.

Before you proceed you should check that all records have been imported correctly. If any records are missing add them now.

When you continue you will be shown two name servers that you need to set as authoritative.

cloudflare nameservers sample

Example of Cloudflare name servers. Your domain could be assigned different ones.

This step is important as without this you cannot manage your domain's DNS via Cloudflare and also cannot use their CDN.

To do this go to your current DNS manager service's control panel and replace the existing name servers with the ones displayed by Cloudflare.

changing to cloudflare nameservers

Example of a name server change being done. Your DNS management console may look different.

When Cloudflare detects the change in a few minutes to an hour, it will change the status of your domain name to Active in Cloudflare.

Switch on CDN Proxy Caching

To use Cloudflare CDN you need to enable Cloudflare proxy caching for your domain/sub-domain.

To do this visit the domain name's control panel in Cloudflare and go to DNS settings.

Locate the CNAME record for your S3 bucket website and edit it to enable proxying.

cloudflare dns enable proxy

Cloudflare CDN will now take care of global requests for all files from your S3 bucket website!

Make it Secure

To keep the data transfer of files secure between Cloudflare's local proxy and the website visitor's web browser you can enable encryption.

To do this visit the SSL/TLS settings and select Flexible mode.

cloudflare ssl flexible mode

The Result

Once you have completed the above steps you should be able to view your S3 bucket website when you try to access it via the assigned domain/sub-domain.

s3 website dns cname ssl

If you have also additionally enabled encryption you should also see the padlock secure icon in your web browser's address bar.