Inventory Existing Owners and Import Support First
Start with an ownership register, not a repository. Every Cloudflare object already has an owner: the dashboard, another Terraform root, a script, or nobody. The register records which one before any HCL is written. Import is an ownership transfer, not a discovery tool.
The dashboard is an owner with no audit trail. A record, ruleset or Access policy edited there is live behavior that no plan describes. Once Terraform owns it, the next apply treats configuration as authoritative and reverts the edit. Note it, then decide whether code should own it.
A second root is the dangerous case. If another state already manages the object, importing it again creates two owners. Both plans look clean, and either one can destroy what the other believes it owns. Transfer ownership through zero owners instead.
A read-only scan is evidence, not ownership. Run cf-terraforming with a token that can read the account and the zone, and treat its output as an inventory. The scan tells you what exists; it cannot tell you who is allowed to change it.
Import support is a property of the resource and the release. cf-terraforming publishes separate generation and import lists, and some entries need an extra identifier such as a setting name. A resource missing from a tool's list may still be importable by hand; check the provider page before calling it impossible.
Record five fields per object. Resource type, account or zone scope, provider id shape, current owner, and whether the selected provider can import it. Add a sixth for secrets, because generation can write values you must not commit.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
A["Object exists in the account or zone"] --> B{"Where is it declared?"}
B -->|"this root"| C["Already owned: change it here"]
B -->|"another root"| D["Transfer ownership through zero owners"]
B -->|"nowhere"| E{"Import supported by the selected provider?"}
E -->|"no"| F["Leave manual, or recreate deliberately"]
E -->|"yes"| G{"Configuration can be generated?"}
G -->|"yes"| H["cf-terraforming generate"]
G -->|"no"| I["Write HCL from the resource schema"]
H --> J["Review scope, id shape and secrets"]
I --> J
J --> K["Import block, then a clean plan"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class A,B,E,G primary;
class C,D secondary;
class H,I,J,K ok;
class F warn;
Each answer has a different next step: decide, transfer, or test an import.
Use Cf-Terraforming or Import Blocks Only for Supported Objects
cf-terraforming is a translator, not an authority. It reads the live API and renders HCL that the provider schema accepts. The output is a draft with generic names, computed values and whatever the API returns. Review it the way you would review a pull request from a stranger.
Generation and import are two commands with two jobs. Generate writes configuration; import writes state or emits import blocks. Running one does not run the other, and neither proves the object is safe to adopt. A missing tool entry narrows your options; the provider's import documentation decides whether adoption is possible.
Scope decides the identifier. A zone-scoped object takes a zone ID, an account-scoped object takes an account ID, and some resources take a composite. Guessing the shape produces a 404 or, worse, an import into the wrong scope. Read the resource page for the exact form.
Generated configuration can carry secrets. Tunnel secrets, origin CA keys and token values may appear in the output. Keep the output out of Git, replace literals with variables, and rotate anything that was already committed. A generated file is not a secret store.
Some generated blocks do not validate. The tool documents that a few resource types produce HCL that needs manual correction, usually because the API shape and the schema disagree at the edges.
The import block is a one-shot instruction. It names the destination address and the provider id, works only in the root module, and makes the provider read the object rather than create it. Remove the block after a successful apply; leaving it in place makes the next plan complain that the resource is already managed.
The order is deliberate. Choose one resource, confirm import support, write or generate HCL, validate it, then import. A mismatch between HCL and the live object does not fail the import; it appears in the next plan as a change or a replacement.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
A["Pick one resource type"] --> B{"In the provider import list?"}
B -->|"no"| B1["Stop: import is unavailable"]
B -->|"yes"| C["Generate HCL or write it by hand"]
C --> D["terraform validate and fmt"]
D --> E["Import block with the documented id"]
E --> F{"Apply outcome"}
F -->|"404 or 403"| F1["Wrong scope or token: no state written"]
F -->|"attribute diff"| F2["HCL does not match the live object"]
F -->|"clean import"| G["Remove the import block"]
G --> H["Plan shows zero changes"]
F1 --> E
F2 --> C
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class A,B,D primary;
class C,E secondary;
class G,H ok;
class B1,F1,F2 danger;
class F warn;
Treat V4-to-V5 as Both HCL and State Migration
A v4 to v5 upgrade changes two things that fail differently. The configuration must describe the v5 schema, and the state must be readable by the v5 provider. A rewritten file does not move state, and a state upgrader does not fix an argument name.
The provider changed foundations. v4 was built on the legacy SDK; v5 uses the Plugin Framework. That changes how nested blocks, optional attributes and schema versions are represented. Since v5.19 the provider ships upgraders that transform old state on the first plan or apply, but coverage is per resource.
Address changes and shape changes are separate mechanisms. A moved block tells Terraform that one address now means another. A MoveState handler, where the provider has one, transforms the record during that move. Without the handler the rename needs a manual remove and import.
Splits and merges are not renames. A split turns one v4 object into a primary resource plus a companion; a merge folds a standalone resource into a parent. Neither has a single destination address, so neither can be expressed as one move.
| Change | v4 to v5 example | Performed by | Evidence in the plan |
|---|---|---|---|
| Attribute shape | zone becomes name, account_id becomes account.id | HCL rewrite | An in-place update, no replacement |
| Resource rename | cloudflare_record becomes cloudflare_dns_record | moved block plus MoveState | Same id, a move annotation, no create or destroy |
| Split | cloudflare_zone_settings_override becomes one cloudflare_zone_setting per setting | Import blocks plus a removed block, then manual state removal | Imports and a companion create, no destroy |
| Merge | cloudflare_workers_secret becomes a secret_text binding | tf-migrate plus manual review | The old address is removed, the script updates, the secret stays attached |
| Manual rename | cloudflare_access_custom_page becomes cloudflare_zero_trust_access_custom_page | State remove plus import | New address, same object id, no replacement |
The rename table is the contract. It marks each rename as automatic or manual, and the manual rows are where rehearsals fail. A plan that shows a destroy for a manual rename is telling you the state was never moved.
Upgrade v4 first. If the source is older than 4.52.5, move to that release and apply before attempting v5. That release writes transitional state that later upgraders expect, and skipping the step produces a failure that looks like a provider bug.
Absence of an upgrader is not silent. A resource without one still imports or moves, but its attributes are read against the new schema and differences appear as a diff. That is why the first plan is evidence about coverage, not a formality.
Rehearse Tf-Migrate on a Backed-Up Copy
Rehearse against a copy of both configuration and state. A branch protects the files; a state snapshot protects the objects' recorded identity. The rehearsal runs against a copy of both, because that is where you discover where the tool stops short.
Pin the tool and record it. tf-migrate's support matrix is version-specific, and the newest target is not automatically the best-tested one. Write the tool release, the v5 target and the v4 source into the change record. This article pins Terraform 1.17.1 and cloudflare 5.28.1, so the rehearsal should use exactly those.
Dry run first. The --dry-run flag prints the rewrite without touching files. Read the summary it prints, then run it for real into a separate output directory so the original tree stays comparable. Backups are on by default; keep them until the change is closed.
Read the diff like a migration, not a formatter. Cross-file references change, data source attributes change, and move blocks appear. Check that every renamed type has a move and every split has an import. Search the migrated files for MIGRATION WARNING, because that is where the tool admits it cannot finish.
The state changes later. tf-migrate does not edit state. The v5 provider upgrades it on the first plan or apply, which is why the rehearsal needs a scratch workspace seeded from the snapshot. Run the plan there, save it, and keep it as evidence.
Use the drift verifier as a second opinion. verify-drift reads plan output and separates documented exemptions from unexpected changes; a non-zero exit means a human still has to look.
Plan for the manual paths. Zone settings split into per-setting resources, Worker secrets merge into the script, and application-scoped Access policies become inline policies. Each has a documented sequence, and each is a place where an automated move stops short.
Keep a real rollback. The old branch, the old lock file and the state snapshot together are the rollback. A git revert alone is not, because state has already been rewritten into v5 shape and the older provider may not read it.
An automated move is not a promise of an empty plan. Computed values refresh, splits create companions and normalizers rewrite a few fields. Expect those; investigate anything else.
Review Normalization, Splits and Replacement Risks
Sort every plan line into one of four buckets. A refresh re-reads a computed value. A normalization rewrites a representation without changing behavior. A split creates a new address for behavior that was previously implicit. A replacement destroys and recreates. Only the last is an outage risk.
Refresh noise is the majority and the least interesting. (known after apply) on timestamps, versions and computed metadata is the provider saying it will re-read the API. It resolves after one apply and should not change traffic.
Normalization is bounded and documented. Gateway policy precedence collapses from internal values to user-facing ones, list CIDRs lose their prefix length, and empty strings, maps and lists become null. These are representation changes, not policy changes, but confirm the resulting value is the one you meant.
Splits deserve their own review. A v4 Argo resource becomes smart routing plus tiered caching, and the companion is created fresh. Zone settings become one resource per setting. A split that creates the companion but leaves the old object in place can apply the same behavior twice.
A replacement is the line that matters. Read the reason, not the count. Scope changes, ForceNew arguments and missing moves all produce the same -/+ marker with different fixes. If the reason is a missing move, fix the address; do not accept the replacement.
Scope changes hide inside renames. Access policies lose zone_id and become account-level, and a policy that only had a zone ID needs a real account ID. A renamed attribute with a different scope is not a rename; it is a new object unless the state is moved and the scope is correct.
A small plan can be worse than a large one. Three hundred computed refreshes are safer than three lines containing one zone replacement.
Do not apply an unexplained destroy. Every destroy should map to an object you intend to stop managing. If it does not, stop and fix the configuration or the state; an apply is not a diagnostic.
Compare Runtime Behavior Before and After Migration
A clean plan proves the records, not the service. Terraform compares state to configuration and the API's read model; it does not prove that a browser resolves the hostname or that a WAF rule still blocks. Capture behavior before the migration and repeat the same probes after.
DNS is the first check because everything else depends on it. Compare the record content, proxy status and TTL at the authoritative nameserver and at a public resolver. A recreated record with the same name but a new id can still serve the wrong target if the content drifted.
TLS is the second. Check the certificate chain, the minimum version and the HSTS header on the edge. A settings split that misses one setting can leave the zone on a different TLS default without any plan line calling it a replacement.
Rules and routes are behavior, not schema. Send the same request that the WAF should block, the same URL that the cache rule should cache, and the same path that the Worker route should serve. Compare status codes, headers and response bodies.
Access is the easiest to break silently. An application that loses its inline policies may still load, just without the challenge. Test as an allowed user and as a denied user, and confirm the policy list on the application rather than only on the policy resource.
Finish with state consistency. The key objects should keep their ids, the state list should have no orphaned addresses, and a fresh plan should be empty apart from documented perpetual differences. If an id changed, explain why before closing the window.
Review a provider migration
Execution status: design-only. The sequence is a rehearsal on a copy; no production state is touched. The versions are Terraform CLI 1.17.1 and cloudflare/cloudflare 5.28.1.
Prerequisites. A root with at least one DNS record, one ruleset and one Worker route; a read-only API token; a state snapshot; and the tf-migrate release you intend to use.
Baseline. Before changing anything, record the provider ids, capture DNS answers from the authoritative and a public resolver, save the TLS certificate chain, send one request that should be blocked and one that should pass, and fetch the Worker route response.
- Copy the root to a rehearsal directory and seed a scratch workspace from the state snapshot.
- Run tf-migrate with
--dry-runand save the diff. - Run the real migration into an output directory and search for
MIGRATION WARNING. - Set the provider constraint to 5.28.1, regenerate the lock file and run a plan to a saved file.
- Classify each line as refresh, normalization, split or replacement; stop on any replacement you cannot explain.
- Apply only the reviewed plan, then run the same probes and compare.
- Check the ids of the zone, record, ruleset and route, and run a final plan.
Expected observation. Same object ids for renamed resources, no destroys, imports for split settings, and identical probe results. One-time computed refreshes are allowed; a changed certificate or a missing block is not.
Failure check. A policy that loads without a challenge means inline policies were lost. A 404 after a scope change means the account ID is wrong. A record with a new id means the move did not happen.
Cost and cleanup. API reads and a few plans; no new Cloudflare objects unless a split companion is created. Delete the rehearsal directory and the saved plan, and keep the state snapshot until the change record is closed.
Close on behavior. The migration is finished when the plan is explainable and the service behaves the same; the provider version is only the mechanism.
Next in the Series
In Cloudflare Modules, Tests and Delivery Pipelines at Team Scale, we'll explore Apply Terraform engineering practices to account/zone/product ownership and observable edge behavior.