AI summary
What TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard. manages on my Cloudflare PagesCloudflare's hosting for static sites. You give it the built files and it serves them from Cloudflare's servers. site, what I left out on purpose, and why GitHub ActionsGitHub's service for running scripts, such as a build and a deploy, automatically when you push to a repository. and WranglerCloudflare's command-line tool for Pages and Workers. It uploads a site's files and stores its secrets. handle the deploys.
- TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard. owns the resources around the site: a KV namespaceA small key-value store on Cloudflare. Here it holds the daily chat counters., a WAFWeb Application Firewall: Cloudflare's rules that check requests before they reach the site. rate-limit ruleA rule that counts each visitor's requests and blocks them for a short time once they go over a set number., an R2 bucketA storage container in R2, Cloudflare's file storage. Here it holds saved conversations. and its lifecycle ruleA rule on an R2 bucket that deletes files automatically once they reach a set age..
- The Pages project stays out of TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard., so its production secretA value such as an API key, stored encrypted in the site's settings. The site's code can read it, but the dashboard never shows it again. can't end up in the state fileTerraform's record of every resource it created, with each one's settings. Terraform compares it with your files to work out what to change..
- Deploys go through GitHub ActionsGitHub's service for running scripts, such as a build and a deploy, automatically when you push to a repository. and WranglerCloudflare's command-line tool for Pages and Workers. It uploads a site's files and stores its secrets., after the build and checks pass.
My site is static and runs on Cloudflare PagesCloudflare's hosting for static sites. You give it the built files and it serves them from Cloudflare's servers.. The chat on the home page added a KV namespaceA small key-value store on Cloudflare. Here it holds the daily chat counters., a rate-limit ruleA rule that counts each visitor's requests and blocks them for a short time once they go over a set number. and an R2 bucketA storage container in R2, Cloudflare's file storage. Here it holds saved conversations. around it.
I wanted those written down in the repo instead of living only in the Cloudflare dashboard. That’s what TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard. does: you describe resources in files, and it creates or updates them to match.
On my site, TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard. manages only those resources. The Pages project and the deploys stay with WranglerCloudflare's command-line tool for Pages and Workers. It uploads a site's files and stores its secrets. and a GitHub ActionsGitHub's service for running scripts, such as a build and a deploy, automatically when you push to a repository. workflow.
What Terraform owns
infra/terraform/main.tf has four resources: the KV namespaceA small key-value store on Cloudflare. Here it holds the daily chat counters. for daily chat counters, the R2 bucketA storage container in R2, Cloudflare's file storage. Here it holds saved conversations. for saved conversations, a lifecycle ruleA rule on an R2 bucket that deletes files automatically once they reach a set age. on that bucket, and the WAFWeb Application Firewall: Cloudflare's rules that check requests before they reach the site. rate-limit ruleA rule that counts each visitor's requests and blocks them for a short time once they go over a set number.. Here’s that rule:
resource "cloudflare_ruleset" "rate_limit" {
zone_id = var.zone_id
name = "fatihinci rate limits"
kind = "zone"
phase = "http_ratelimit"
rules = [
{
description = "Limit bursts to the chat API"
expression = "(http.request.uri.path eq \"/api/chat\")"
action = "block"
ratelimit = {
characteristics = ["cf.colo.id", "ip.src"]
period = 10
requests_per_period = var.chat_requests_per_10_seconds
mitigation_timeout = 10
}
},
]
}The Free plan fixes the period and the block duration at 10 seconds, and gives you one rule that counts by IP and data center. The limit of 5 requests per 10 seconds is my own choice.
The lifecycle ruleA rule on an R2 bucket that deletes files automatically once they reach a set age. is the one I like most. It deletes saved chats after 90 days, so the retention period on the privacy page is enforced by the infrastructure and doesn’t depend on me remembering.
What I left out on purpose
The Pages project itself isn’t in TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard.. Its settings hold the production API key the chat uses to call Claude.
TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard. keeps a state fileTerraform's record of every resource it created, with each one's settings. Terraform compares it with your files to work out what to change.: its record of everything it created, with each resource’s settings. If TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard. managed the Pages project’s settings, the key could end up in the state fileTerraform's record of every resource it created, with each one's settings. Terraform compares it with your files to work out what to change.. A careless terraform apply, the command that makes the changes, could also remove the key.
So the key stays a WranglerCloudflare's command-line tool for Pages and Workers. It uploads a site's files and stores its secrets. secretA value such as an API key, stored encrypted in the site's settings. The site's code can read it, but the dashboard never shows it again.. My state fileTerraform's record of every resource it created, with each one's settings. Terraform compares it with your files to work out what to change. holds nothing sensitive.
The site’s own settings live in wrangler.toml. Its bindings are the names the site’s code uses to reach the KV namespaceA small key-value store on Cloudflare. Here it holds the daily chat counters. and the R2 bucketA storage container in R2, Cloudflare's file storage. Here it holds saved conversations..
Deploys aren’t Terraform’s job
I looked into deploying the site with TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard. too. My Pages project is a Direct Upload project: I upload the built files myself, instead of Cloudflare building them from a Git repo.
I couldn’t find a way to upload files to a Direct Upload project with the Cloudflare providerThe Terraform plugin for one service's API. The Cloudflare provider is what lets Terraform create Cloudflare resources.. Cloudflare’s own guide for deploying from GitHub ActionsGitHub's service for running scripts, such as a build and a deploy, automatically when you push to a repository. and similar tools uses wrangler pages deploy for that. And you can’t later switch a Direct Upload project to one that Cloudflare builds from Git.
Since providerThe Terraform plugin for one service's API. The Cloudflare provider is what lets Terraform create Cloudflare resources. v5.11, TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard. can upload static assets to a WorkersCloudflare's service for running your own code on its servers. A Workers script can also serve a site's static files. script. So if you want one tool for everything, the route is moving the site to WorkersCloudflare's service for running your own code on its servers. A Workers script can also serve a site's static files..
I didn’t take it. Fixing a typo in an article shouldn’t need a terraform apply.
Instead, a GitHub ActionsGitHub's service for running scripts, such as a build and a deploy, automatically when you push to a repository. workflow runs on every push to main. It installs the dependencies with npm ci (why npm ci and not npm install), builds the site, runs the checks, and only then calls WranglerCloudflare's command-line tool for Pages and Workers. It uploads a site's files and stores its secrets..
- Push
A commit lands on main and starts the workflow.
- Build
The site is built and the type checks run.
npm run build - Verify
The checks cover links, each page's link to its other-language version, and the data search engines read from each page. If they fail, nothing ships.
npm run verify - Deploy
Wrangler uploads the build with a token that can only edit Pages.
wrangler pages deploy
Running plan and apply
cd infra/terraform
export CLOUDFLARE_API_TOKEN=...
terraform init
terraform plan
terraform applyterraform init downloads the Cloudflare providerThe Terraform plugin for one service's API. The Cloudflare provider is what lets Terraform create Cloudflare resources.. You run it once in a new folder, before the first plan.
terraform plan shows what would change, without changing anything. Read the plan before you apply. My first one, before the R2 bucketA storage container in R2, Cloudflare's file storage. Here it holds saved conversations. existed, said 2 to add, 0 to change, 0 to destroy. The destroy count is the one I check first.
My state fileTerraform's record of every resource it created, with each one's settings. Terraform compares it with your files to work out what to change. is local and gitignored. There are no secretA value such as an API key, stored encrypted in the site's settings. The site's code can read it, but the dashboard never shows it again.s in it, but keep it safe anyway. Without it, TerraformA tool that creates and updates cloud resources from files you keep in your repo, so the setup doesn't live only in a dashboard. no longer knows what it created. You’d have to hand each resource back to it with terraform import.
