AI summary

What Terraform manages on my Cloudflare Pages site, what I left out on purpose, and why GitHub Actions and Wrangler handle the deploys.

  • Terraform owns the resources around the site: a KV namespace, a WAF rate-limit rule, an R2 bucket and its lifecycle rule.
  • The Pages project stays out of Terraform, so its production secret can't end up in the state file.
  • Deploys go through GitHub Actions and Wrangler, after the build and checks pass.

My site is static and runs on Cloudflare Pages. The chat on the home page added a KV namespace, a rate-limit rule and an R2 bucket around it.

I wanted those written down in the repo instead of living only in the Cloudflare dashboard. That’s what Terraform does: you describe resources in files, and it creates or updates them to match.

On my site, Terraform manages only those resources. The Pages project and the deploys stay with Wrangler and a GitHub Actions workflow.

What Terraform owns

infra/terraform/main.tf has four resources: the KV namespace for daily chat counters, the R2 bucket for saved conversations, a lifecycle rule on that bucket, and the WAF rate-limit rule. Here’s that rule:

hcl
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 rule 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 Terraform. Its settings hold the production API key the chat uses to call Claude.

Terraform keeps a state file: its record of everything it created, with each resource’s settings. If Terraform managed the Pages project’s settings, the key could end up in the state file. A careless terraform apply, the command that makes the changes, could also remove the key.

So the key stays a Wrangler secret. My state file 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 namespace and the R2 bucket.

Who owns whatPick a tool to see its part of the site.

Terraform

Things that rarely change and should be reviewed.

  • KV namespace
  • R2 bucket and lifecycle rule
  • WAF rate-limit rule

Wrangler

Settings that belong with the site's code.

  • wrangler.toml bindings
  • API key secret
  • Upload to Pages

GitHub Actions

Runs on every push to main.

  • Build
  • Verify
  • Call Wrangler

Deploys aren’t Terraform’s job

I looked into deploying the site with Terraform 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 provider. Cloudflare’s own guide for deploying from GitHub Actions 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 provider v5.11, Terraform can upload static assets to a Workers script. So if you want one tool for everything, the route is moving the site to Workers.

I didn’t take it. Fixing a typo in an article shouldn’t need a terraform apply.

Instead, a GitHub Actions 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 Wrangler.

From push to liveScrub through it, or press play.
  1. Push

    A commit lands on main and starts the workflow.

  2. Build

    The site is built and the type checks run.

    npm run build
  3. 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
  4. Deploy

    Wrangler uploads the build with a token that can only edit Pages.

    wrangler pages deploy

Running plan and apply

sh
cd infra/terraform
export CLOUDFLARE_API_TOKEN=...
terraform init
terraform plan
terraform apply

terraform init downloads the Cloudflare provider. 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 bucket existed, said 2 to add, 0 to change, 0 to destroy. The destroy count is the one I check first.

My state file is local and gitignored. There are no secrets in it, but keep it safe anyway. Without it, Terraform no longer knows what it created. You’d have to hand each resource back to it with terraform import.

References

  1. Terraform: terraform init command
  2. Cloudflare Terraform provider
  3. Cloudflare: Use Direct Upload with continuous integration
  4. Cloudflare: Direct Upload
  5. Cloudflare changelog: Deploy full-stack apps on Workers using Terraform
  6. Cloudflare: Rate limiting rules
  7. Cloudflare: R2 object lifecycles

Written by:

Full Stack Software EngineerSamsun, Türkiye

Full Stack Software Engineer. I started with client sites at an agency in 2019, then worked on embeds, a dashboard and analytics at a startup called Popupsmart.