This setup is great in many ways; that said, it has two limitations that became a real pain in my specific case:

  1. The information in S3 logs is pretty limited when you want to do any real analysis. For instance, the X-Forwarded-For header is ignored, which hides visitors' real IP addresses when your bucket sits behind a proxy like CloudFlare.
  2. The pricing model based on the number of S3 requests is not always cost-effective, depending on how well CloudFlare's cache hits. In my case, I run two websites, and one of them gets an unusually high number of S3 requests even with CloudFlare in front.

A slightly more complete architecture

To work around those limitations, there's an easy piece we can add to our existing stack: AWS CloudFront.

In short, CloudFront is a Content Delivery Network (CDN) that lets visitors connect to a server closer to them, and usually faster, by caching files from your origin (an S3 bucket in our case) on edge servers around the world.

Pricing is based primarily on bandwidth used, plus a flat rate per large chunk of HTTP requests regardless of the HTTP method.

Adding CloudFront

Plain text
Nous allons passer de ceci:               À cela:

+-----------------------------+           +-----------------------------+
|          Interwebz          |           |          Interwebz          |
+--------------+--------------+           +--------------+--------------+
               |                                         |
+--------------v--------------+           +--------------v--------------+
|  Cloudflare DNS+Proxy+CDN   |           |  Cloudflare DNS+Proxy+CDN   |
+--------------+--------------+           +--------------+--------------+
               |                                         |
+--------------v--------------+           ###############v###############
|   AWS S3 Website endpoint   |           ####### AWS Cloud Front #######
+--------------+--------------+           ###############+###############
               |                                         |
+--------------v--------------+           +--------------v--------------+
|        AWS S3 Bucket        |           |   AWS S3 Website endpoint   |
+-----------------------------+           +--------------+--------------+
                                                         |
                                          +--------------v--------------+
                                          |        AWS S3 Bucket        |
                                          +-----------------------------+

How to set up CloudFront with an S3 bucket in the AWS Console

Here is a step-by-step walkthrough for creating a CloudFront distribution (or CDN property):

  1. Click CloudFront and then Create Distribution
  2. Under the Web section, click Get Started
  3. For Origin Domain Name, don't use the dropdown list! Instead, copy-paste the full static website hostname of your S3 bucket, also known as the Endpoint.

You'll find it under the Static Website Hosting section of your bucket properties. The goal here is to keep letting S3 serve the static files with its default index and error document rules intact. That way, all your URLs pointing to folders (commonly called pretty URLs) will keep working properly.

  1. Change the Object Caching settings manually so you have full control over your costs based on your expected traffic.

The longer the TTL (time to live), the less often CloudFront has to re-fetch files from your S3 bucket. For a static website, don't be shy about cranking it up.

  1. Pick Only US and Europe to save money if, like me, you're in Canada and don't really care about traffic from Asia.
  2. Don't forget to add yourdomainname.com and www.votrenomdedomaine.com to CNAMEs, otherwise AWS won't know how to route your domain to your distribution.
  3. Leave everything else at the default values and click Create Distribution.
  4. Wait until your distribution's status changes to Deployed as it can take a good while.

Pointing traffic to CloudFront instead of S3 Website Hosting

We're all done on the AWS side, so now we need to update CloudFlare to point our domains at CloudFront instead of S3:

  1. Log in to CloudFlare, select your domain, and open the DNS tab
  2. Update your DNS records so you have two CNAME (alias) records, one with www and one without, pointing to your CloudFront domain, which looks something like pleindecaracteres.cloudfront.net. You'll find it in your CloudFront distributions list.
  3. You can leave the status set to DNS and HTTP Proxy (CDN) for now; we'll change that later.

Visually, it should look like this:

Configuration DNS de Cloudflare pointant vers CloudFront

Be nice to Google and help your SEO

One thing not to forget if you followed Claudine's tutorial is making sure www.nomdedomaine.com still redirects to yourdomainname.com, because the steps above just broke that redirect. The reason is that CloudFlare now sends all requests to our single top-level bucket.

To fix that in CloudFlare, simply add a Page Rule of type Forwarding URL (301) redirecting https://www.votrenomdedomaine.com/* to https://votrenomdedomaine.com/.

Oh! All my examples use HTTPS because I turned on SSL in Flexible mode. You should too. Google is in love with SSL and will reward you with bonus SEO points.

Next steps

Your website is now served through CloudFront, which means your bill will scale much more gently if traffic picks up. CloudFlare isn't the cheapest option on the market if you're a massive player, but for a site with modest traffic, it's wonderful.

In part two, we'll turn on logging and analyze our famous traffic with GoAccess.