Enabling CloudFront logging (recording every request)

To view accurate HTTP request stats with a tool like GoAccess, we first need to turn on logging in CloudFront. Here is how to do it from the AWS Console.

First, create an S3 bucket where all the log files will be delivered:

  1. From the main menu, click S3.
  2. Click Create a bucket.
  3. Enter a name like myname-cloudfront-logs.
  4. Pick a region that makes sense for you.

Next, enable log delivery so CloudFront sends its logs to that S3 bucket:

  1. From the Services menu, click CloudFront.
  2. Click on the distribution you want to configure.
  3. In the General tab, click Edit.
  4. Turn Logging on by selecting On.
  5. Select the bucket you just created.
  6. Add a Log Prefix matching your site so you can keep logs from multiple sites in a single bucket (e.g. yourdomainname.com/).
  7. You can leave Cookie Logging off, since a static site rarely needs it.
  8. Click Yes, Edit to save your changes.

You now have a CloudFront distribution logging every single request and dropping the files into your S3 bucket.

Installing GoAccess

If you're on Linux or macOS, just install GoAccess as described on their website; otherwise, grab my Vagrant setup, which pre-installs everything you need.

Here is the link to the Vagrant setup: https://github.com/fredbourni/vagrant-goaccess-awscf

If you install it yourself, you'll need to tweak GoAccess's default config so it understands the CloudFront log format. Or you can make life easy and download and patch it with cloudfront.diff (exact command: patch goaccess.conf cloudfront.diff).

Downloading your log files locally

First, configure the AWS CLI so it has access to your logs. Then, using the Vagrant VM in this example, run these commands to pull all the log files down locally:

Run these commands:

Bash
$ cd /vagrant
$ mkdir logs
$ cd logs
$ aws s3 sync s3://myname-cloudfront-logs ./

That will download all the log files from your bucket so GoAccess can crunch them.

Analyzing CloudFront logs with GoAccess

GoAccess lets you view a report right in the terminal or generate a self-contained HTML report you can open in your favourite browser. Let's generate your first HTML report…

Bash
 $ zcat /vagrant/logs/yoursite.com/*.gz | goaccess -o /usr/share/nginx/html/yoursite.html

Then just point your browser to http://localhost/yoursite.html

To run reports in the terminal or play around with GoAccess some more, check out their docs for all the details: https://goaccess.io/faq, https://goaccess.io/man.

Disabling CloudFlare caching

CloudFlare is most often used as a CDN, a proxy that caches requests around the globe to save you a ton of origin hits, at least in theory. The catch is that if CloudFlare caches your HTTP requests, only a fraction of them ever reach CloudFront, which means you're only analyzing a fraction of your actual traffic.

So you need to turn off the proxy feature by clicking the cloud icon in the DNS section for every domain pointing to CloudFront (usually yoursite.com and www.votresite.com). Once you see a grey cloud with an arrow bypassing it, you're all set. CloudFlare is still handling your DNS, but it's no longer sitting as a CDN between CloudFront and your visitors. Every single request now goes straight to AWS, letting you analyze every visit, and obviously every visitor.

Oh, I almost forgot about SSL! If your site uses HTTPS, make sure you have an SSL certificate set up in AWS and attached to your CloudFront distribution, as explained on this French blog. By the way, it's been free for quite a while now!