Easy Cloudfront cache-busting with Travis-CI

By Patrick McDavid, October 16, 2017

Update February 27, 2018: There’s a better workflow offered here

This site is hosted on AWS’ S3 via Cloudfront which is an easy way to keep it online with minimal concern at runtime. We’re trying to get more contributors to this project in support of our OpenAPI docs, this blog, and other odds and ends.

Cloudfront is good both for speed, but more importantly for us, for it’s custom SSL offering, which ensures that the content you see on the site came from us.

A side-effect of this from our perspective is the default 24-hour cache that Cloudfront uses in their CDN. This meant that changes to this site could take up to a day before they’d go live without some manual intervention.

The optimal solution would be to invalidate the caches on just what’s changed on a publish event. Another option would be to invalidate the whole cache.

We chose a one-liner: configure our S3 objects to have a TTL of a few minutes. Adding just one line to our .travis.yml achieves our desired affect, the site shows the latest information within a couple minutes.

  cache_control: "max-age=120"

You can read the pertinent travis-ci docs here.

A downside here is that your cache is going to miss more often. Depending on the traffic to your site this will affect latency somewhat. For our use case, this wasn’t a major concern, at least for now.

Too Easy