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

**URL:** <https://discourse.gohugo.io/t/hedgerules-program-to-deploy-cloudfront-functions-for-use-with-s3-rest-api/56828>\
**Category:** tips & tricks\
**Created:** [March 8, 2026, 7:53pm UTC](https://discourse.gohugo.io/t/hedgerules-program-to-deploy-cloudfront-functions-for-use-with-s3-rest-api/56828 "2026-03-08T19:53:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrled](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mrled/32/13738_2.png) [@mrled](https://discourse.gohugo.io/u/mrled)\
**Post date:** [March 8, 2026, 7:53pm UTC](https://discourse.gohugo.io/t/hedgerules-program-to-deploy-cloudfront-functions-for-use-with-s3-rest-api/56828/1 "2026-03-08T19:53:27Z")

</div>

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](https://hedgerules.micahrl.com/docs/headers/request-path-tokens/) 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](https://hedgerules.micahrl.com/docs/development/kvs-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](https://hedgerules.micahrl.com/docs/aws/pricing/) 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](https://me.micahrl.com/) for a week or two, as well as the [Hedgerules](https://hedgerules.micahrl.com/) documentation site.

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