This guide walks you through deploying a Hugo site to AWS CloudFront using Hedgerules.
Prerequisites#
- Hugo installed
- An AWS account with permissions for S3, CloudFront, and CloudFront KeyValueStore
- AWS CLI configured
1. Install Hedgerules#
go install github.com/mrled/hedgerules/hedgerules/cmd/hedgerules@latest2. Add the _hedge_headers.json template#
Copy the index.hedgeheaders.json layout from the Hedgerules example site into your Hugo site’s layouts/ directory. This template generates a _hedge_headers.json file in your build output that maps URL paths to HTTP response headers.
You also need to register the output format in your hugo.toml:
[outputs]
home = ["HTML", "hedgeheaders", "hedgeredirects"]The output format definitions are provided by the theme. If you’re not using the theme, see the Configuration Reference for the full output format config.
3. Configure headers in hugo.toml#
Add global default headers under the root / key:
[params.HedgerulesPathHeaders]
[params.HedgerulesPathHeaders."/"]
X-Content-Type-Options = "nosniff"
X-Frame-Options = "DENY"These apply to every response. Override them for specific pages using frontmatter:
---
title: "My Page"
HedgerulesHeaders:
Cache-Control: "max-age=3600"
---4. Build your Hugo site#
hugoThis produces public/ with _hedge_headers.json and _hedge_redirects.txt alongside your HTML.
5. Deploy edge rules#
hedgerules deploy --site public/This creates (or updates) the CloudFront Functions and KVS entries for redirects and headers.
6. Deploy site content#
hugo deploy --target mysiteThis uploads your site to S3 and invalidates the CloudFront cache.
What Hedgerules does#
When you run hedgerules deploy, it:
- Scans directories in your build output to generate index redirects (
/path->/path/) - Reads
_hedge_redirects.txtfor Hugo alias redirects and custom redirects - Reads
_hedge_headers.jsonfor custom response headers - Uploads redirect entries to a CloudFront KVS (used by the viewer-request function)
- Uploads header entries to a CloudFront KVS (used by the viewer-response function)
The CloudFront Functions handle the actual request/response processing at the edge.