A Cloudflare zone managed only through the dashboard has no history, no review step, and no honest answer to “what changed?”. You do not need a platform team to fix that. Step one is a scoped API token and two curl commands that snapshot DNS records and zone settings into files you can diff — that alone transforms incident response. Step two, when the zone matters enough, is the Terraform provider: config in git, changes as reviewed plans, and a terraform plan that doubles as drift detection.
What’s happening
The API exposes essentially everything the dashboard does: zones, DNS records, zone settings, rulesets, certificates. Two authentication schemes exist — the legacy Global API Key, which grants everything everywhere, and API tokens, which are scoped to specific permissions on specific zones and are the only thing you should create in 2026. A token that can read one zone’s DNS cannot delete another zone’s WAF rules; that asymmetry is the entire security model of automation.
Terraform builds on the same API. The cloudflare/cloudflare provider (v5, generated from Cloudflare’s OpenAPI schemas since early 2025) maps zone resources — DNS records, settings, rulesets, tunnels — into HCL. For existing zones, the cf-terraforming tool generates resource definitions and import commands from live config, so adoption does not start with retyping five years of dashboard decisions. Two honest caveats: newer or niche products sometimes lag in provider coverage, and Terraform state contains your zone’s full config — treat the state file with backup-and-secrets discipline.
What to check first
- Who has the Global API Key today. Every script using it is an incident waiting for a laptop theft. Inventory before you automate.
- Whether the zone is drift-prone. Multiple admins clicking in the dashboard means any exported config starts rotting immediately; agree on the source of truth before writing it down.
- Account versus zone scope. DNS records and settings hang off the zone; Workers, tunnels, Load Balancing pools, and audit logs hang off the account. Your token needs the right scopes at the right level.
How to verify
Create a token in the dashboard (My Profile, API Tokens) with the minimum permission set — for snapshots, Zone Read and DNS Read on the one zone — then prove it works and see exactly what it can do:
# Is the token valid?
curl -s -H "Authorization: Bearer <api-token>" \
"https://api.cloudflare.com/client/v4/user/tokens/verify"
# Find the zone id
curl -s -H "Authorization: Bearer <api-token>" \
"https://api.cloudflare.com/client/v4/zones?name=example.com"
# Snapshot 1: every DNS record, BIND zone-file format
curl -s -H "Authorization: Bearer <api-token>" \
"https://api.cloudflare.com/client/v4/zones/<zone-id>/dns_records/export" \
> dns-$(date +%F).txt
# Snapshot 2: every zone setting (TLS mode, cache level, HSTS, ...)
curl -s -H "Authorization: Bearer <api-token>" \
"https://api.cloudflare.com/client/v4/zones/<zone-id>/settings" \
> settings-$(date +%F).json
Commit both files to a repo. From now on, “what changed since last month” is git diff, and the pre-change snapshot in your rollback plan is one command.
The runbook
-
Mint scoped tokens per purpose, not per person. One read-only token for snapshots and monitoring, one write token for the deploy pipeline, each restricted to the zones it serves, each with an expiry where the workflow allows. Store them in a secrets manager, never in the repo, and rotate on a calendar.
-
Automate the snapshot before anything else. A daily cron running the two exports above into git gives you drift detection for free — the day’s diff is either empty or a conversation. This is worth doing even if Terraform never happens.
-
Bootstrap Terraform from live config. Install the provider and let
cf-terraforminggenerate what exists:# Generate HCL for current DNS records, then the matching import commands cf-terraforming generate --resource-type cloudflare_dns_record --zone <zone-id> > dns.tf cf-terraforming import --resource-type cloudflare_dns_record --zone <zone-id> -
Keep the provider config minimal and the token in the environment:
terraform { required_providers { cloudflare = { source = "cloudflare/cloudflare" version = "~> 5" } } } provider "cloudflare" {} # reads CLOUDFLARE_API_TOKEN resource "cloudflare_dns_record" "www" { zone_id = var.zone_id name = "www.example.com" type = "A" content = "203.0.113.10" proxied = true ttl = 1 # 1 = automatic, required for proxied records } -
Make
terraform plana review gate and a drift alarm. Plans run in CI on every pull request; a scheduled plan against an unchanged branch catches dashboard edits nobody mentioned. Non-empty scheduled plan output is a page-worthy signal on a zone that claims to be code-managed. -
Decide the split and write it down. All-in Terraform, or Terraform for DNS and rulesets with the dashboard left for exploratory toggles — both work, but only if the boundary is explicit. The worst state is two sources of truth, each half-trusted.
When this approach is wrong
For a personal blog on a Free zone with one admin, the daily snapshot is plenty — Terraform’s ceremony costs more than the drift it prevents. If the team will not stop making dashboard changes, importing to Terraform manufactures permanent drift conflict; fix the working agreement first. And do not hand-write HCL for a 300-record zone that cf-terraforming can generate — typo-driven outages during adoption are the classic own-goal.
Common mistakes
- Automating with the Global API Key because it was already in a wiki somewhere. One leak equals every zone in the account.
- Tokens with account-wide write scopes “to be safe”. The blast radius you tolerate in a token is the blast radius you will eventually get.
- Committing tokens or
terraform.tfvarsto the repo. Environment variables and secret stores exist; use them. - Importing resources without pinning the provider version. Major provider versions rename resources and fields (v4 to v5 did exactly that:
cloudflare_recordbecamecloudflare_dns_record); an unpinned upgrade mid-quarter is an avoidable fire. - Letting the snapshot job rot. An export that silently stopped in March is discovered during an incident in July. Alert on job failure, not just on diffs.
Prevention / maintenance checklist
- Token inventory reviewed quarterly: purpose, scope, last used, expiry — unused tokens revoked.
- Daily DNS and settings snapshots committed to git, with failure alerting on the job.
- Provider version pinned; upgrades done deliberately with the changelog open.
- Scheduled drift plan (or snapshot diff) feeding the ops channel weekly.
- State file stored remotely with encryption and restricted access, treated as sensitive as the token that built it. Full API reference: developers.cloudflare.com/api/.
A Cloudflare migration with a written rollback plan, or a zone that gets reviewed quarterly instead of drifting — that is the operating discipline Stack Harbor sells under Managed Cloud Ops; book an infrastructure review.