Hedgerules: program to deploy CloudFront Functions for use with S3 REST API

I wrote a program designed to provide index rewrites, directory redirects, custom redirects, and custom headers when deploying to AWS with hugo deploy.

  • Index rewrites: when using S3 REST API (instead of S3 static website hosting), requests to /path/ don’t return the contents of /path/index.html by default. This fixes that.
  • Directory redirects: also when using S3 REST API, if there is a /path/index.html, a request to /path will 404 because it doesn’t end in a trailing slash. This fixes that too.
  • Custom redirects: Hugo aliases, and other site-wide redirects, like Netlify’s _redirects file
  • Custom headers: Set HTTP headers on responses, like Netlify’s _headers file
    • It supports a special token to allow a header value that depends on the client’s request path. I use this to support Onion-Location.

It uses CloudFront Functions with KVS. KVS has some limitations, so this may not be suitable for very large sites. I spent some time optimizing though so that it uses as few KVS entries as it can. This enables using S3 REST API (rather than S3 websites) with Cloudfront, which has a few minor benefits like private buckets if you like.

KVS API operations do cost a bit of money at $0.03/1,000,000 read calls, and $1.00/1,000 other calls, including a single call to batch 50 writes. The math is a little long to reproduce here, but for my personal site deploying new content consumes about 20 API calls and I get so little traffic that the reads are just a penny or two.

After deploying the required AWS infrastructure (S3, CloudFront distribution, etc), all you have to do is:

  • Deploy the site with hugo deploy
  • Set the CloudFront KVS hedgerules deploy

I moved off of Netlify in order to use more powerful analytics from AWS and wrote this tool to fill in the gaps. I’ve been using it on my main website for a week or two, as well as the Hedgerules documentation site.

Would love to get some feedback on this: good, bad, just curious what people think.