A minimal template for hosting static files on Cloudflare Workers. Terraform manages the Worker, its custom domains, and the zone's HTTPS redirect. There is no build step or CI pipeline: Cloudflare serves the files in site/ as-is.
- Terraform 1.5 or later
- A Cloudflare account with your domain configured as an active zone
- A Cloudflare API token with permission to manage Workers scripts, custom domains, and the zone setting
-
Replace the contents of
site/with your own static site files. Keep the homepage atsite/index.html. -
Update canonical URLs, metadata,
robots.txt, andsitemap.xmlto use your domain. -
Create a
.envfile in the repository root. It is gitignored; do not commit it.CLOUDFLARE_API_TOKEN=your-api-token TF_VAR_cloudflare_account_id=your-account-id TF_VAR_cloudflare_zone_id=your-zone-id TF_VAR_domain=example.com TF_VAR_worker_name=my-static-site
Terraform reads TF_VAR_<variable_name> environment variables as input variables. domain should be your apex domain; this configuration also attaches www.<domain> to the same Worker.
Initialize Terraform once:
terraform initFor convenience, define this function in your current Bash or Zsh session:
tfapply() (
set -e
set -a
. ./.env
set +a
terraform apply -input=false "$@"
)From the repository root, run:
tfapplyTerraform displays its plan and asks for confirmation. To skip the confirmation intentionally, use tfapply -auto-approve.
After initial setup, replace or edit files in site/ and run tfapply again. Plain HTML, CSS, JavaScript, images, and other static assets need no separate build or CI step.
- Terraform state is local by default and gitignored. Keep it safe; Terraform needs it to manage future changes.
- Keep
.envand Terraform state out of version control. - The
wwwhostname must be available in the same Cloudflare zone as the apex domain.