Skip to content

Adopt Terraform to Manage Cloudflare Infrastructure and Improve Reliability #1207

@jaypatrick

Description

@jaypatrick

Background

Currently, Cloudflare resources (KV namespaces, D1 databases, R2 buckets, Queues, Pipelines, Durable Objects, etc.) are provisioned manually (CLI/dashboard), and then bound through hardcoded IDs or environment variables in the wrangler.toml. This leaves infrastructure reproducibility, disaster recovery, staging/preview parity, and auditability at risk as the project (and the wrangler config) grows.

Proposal: Adopt Terraform for Cloudflare Resource Management

Terraform is an "Infrastructure as Code" tool that lets you declare all cloud resources in version-controlled files, similar to source code. It would bring the following benefits:

1. Easy, Reproducible Cloudflare Setup

  • All resources (KV, D1, R2, Queues, Hyperdrive, Access, etc.) declared once in code — no more one-off CLI runs
  • Spin up new staging, dev, or preview environments with terraform apply -var="env=staging"
  • Disaster recovery is automatic: run Terraform in any account and it provisions all the Cloudflare plumbing

2. Audit & Drift Detection

  • All resource changes tracked in git (who/what/why), facilitating PR-based reviews
  • Running terraform plan flags accidental/undocumented changes made via the dashboard/console

3. Consistent Security & Policy

  • Codify Zero Trust (Access, WAF, etc.), CORS, Workers secrets, and required analytics/pipelines
  • Make it impossible to ship with missing policies, keys, or misconfigured environments
  • Use code review for policy changes (no dashboard click-ops)

4. Reduces Human Error

  • Secrets and IDs are outputs from Terraform, never copied by hand
  • Staging/prod drift or missed steps in onboarding eliminated
  • Supports secret rotation best practices (via manager/CI integration)

5. Documentation & Onboarding

  • New contributors read terraform/README.md and can understand the entire cloud architecture
  • One command spin-up for new environments (including for preview branches)
  • Up-to-date doc for all environment variables and secret scopes

Where Terraform Fits (and What It Won't Do)

  • Terraform will provision all Cloudflare resources (KV, R2, D1, Queues, pipelines, access policies, DNS, Workers secrets, etc.)
  • Wrangler will still be used for Worker code deployment, Durable Object migrations, and D1 table schema migration
  • Schema migrations: Terraform can only create the D1 database itself, not run its SQL migrations — that's still Wrangler's job.
  • Secrets: Terraform can create secret bindings, but initial values should be managed securely (use CI env vars, not committed tfvars).

Additional Potential Value

  • Allows for full dev/staging/production parity of infra
  • Supports future multi-cloud (if needed)
  • Enables infra cost estimation, tagging, and more granular access control if you have multiple accounts
  • Easily extend to manage Vercel/AWS/Deno Deploy infra if the project expands to them

Next Steps (For Non-Terraformers)

  • I'll help set up a basic terraform/ directory and a minimal config to manage the existing Cloudflare resources, with comments.
  • Any git user can apply/revert changes — no vendor lock-in.
  • Documentation and PR review workflows come included, you won't need to become a Terraform expert to benefit.

Let me know if you have further questions or want to see a working PR example for your repo. This change is a best practice for growing serverless projects and will make scaling, recovery, and policy tracking much more robust.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions