Importing live Cloudflare and GitHub settings into OpenTofu without changing them
We moved the settings of two services we run on, Cloudflare and GitHub, from dashboards into OpenTofu code, without changing anything that was live. This log describes how we did it over November 8 and 9, 2026, what the import plans showed, where the tools fell short, and the rules we now follow. The work was done with OpenTofu 1.13.9, the Cloudflare provider 5.28.1, the GitHub provider 6.13.0 and cf-terraforming 0.30.0. Every number below comes from a plan or apply run recorded in our repositories; the references at the end name them.
Background
ZIV LABS runs a brand site, a developer tools site, five browser games, a video publishing job and the automation that builds them. Their infrastructure was set up by hand: a DNS zone, R2 buckets, a Pages project, a D1 database, Web Analytics, and ten GitHub repositories with their branch rules, environments and variables. Nothing recorded why a setting was the way it was, and a new repository or record meant clicks that nobody reviewed. We wanted every change to go through a pull request, with a plan someone reads before it is applied.
The constraint was strict: the sites were live, so the import had to change nothing. Our target for each import was a plan that said 0 to change, 0 to destroy.
Options we weighed
- Where the state lives. We put it in a new, dedicated R2 bucket, separate from the bucket that holds videos, with no lifecycle rule on it, and reach it through OpenTofu's S3 backend.
- How CI acts on GitHub. A GitHub App rather than a personal token: it belongs to the organization, its token is short-lived, and its permissions are listed in one place.
- Who approves an apply. A GitHub environment with the owner as the required reviewer. On
main, CI makes a plan and saves it; the apply job waits for approval and applies exactly that saved plan. - Who owns Workers. Each Worker's routes and custom domains are already code, in that repository's
wrangler.jsonc, deployed on merge. Managing them in OpenTofu too would make two tools rewrite the same routes, so we left them where they are and only listed them in the inventory.
What we did
An inventory first. A read-only workflow listed everything the API token could see. We sorted it with the owner into three groups: import, leave alone, and delete. Two things turned out to belong to other projects (a zone for another website, and a staging environment of a different product); they stay outside this code, and nothing was deleted.
Generated drafts, curated by hand. cf-terraforming writes resource blocks and import blocks from the live account. We ran it in CI and used its output as a draft: we removed the resources we had decided not to manage, renamed the generated names, and added prevent_destroy to anything whose loss would break a site or lose data.
Imports in small pull requests. Each import was its own pull request, and its plan was the review:
| Import | Plan or apply result |
|---|---|
| Zone, 11 DNS records, 2 R2 buckets, Web Analytics | 15 to import, 0 to change, 0 to destroy (after one fix, below) |
| R2 lifecycle rules of the video bucket | 1 to add, 0 to change (could not be imported, below) |
| Pages project, its custom domain, a D1 database | 3 imported, 0 changed, 0 destroyed |
| 10 repositories, their labels, branch rules, 2 environments, 3 variables | 119 imported, 10 changed, 0 destroyed |
| Branch rules switched on for every repository | 5 added, 5 changed, 0 destroyed |
The "10 changed" of the GitHub import were two attributes that exist only in OpenTofu's state (whether to archive a repository on destroy, and how vulnerability alerts are read); nothing changed on GitHub. After each import we removed the import blocks in a follow-up pull request, whose plan said there was nothing to change.
Where it did not go as planned
- The generator ran with no client in CI. Our review of the first workflow found that cf-terraforming 0.30.0 skips creating its Cloudflare client when the
CIenvironment variable is set, which GitHub Actions always sets. We unset it for that one step. - The generator needed an initialized directory. It reads the provider schema through
tofu, which asked for our S3 backend. We run it in a scratch directory that has only the providers and the same lock file. - One generated import ID was wrong. For the D1 database it produced the account ID twice instead of the account and database IDs. We took the database ID from the Worker's own configuration.
- Some things cannot be imported. The provider does not implement import for R2 lifecycle rules, and the generator could not produce them. We wrote the same two rules by hand from the inventory, so the first apply wrote what was already there.
- A first plan wanted a change. The Web Analytics site planned an in-place update on import. We told OpenTofu to ignore the two fields it reported, which brought the plan to zero changes. We did the same for computed fields of the Pages project and the D1 database: OpenTofu manages that they exist, not their computed values.
- Reading an environment needs more than the environment permission. With the GitHub App granted Environments (read and write), reading
infra-applystill returned 403. It also needed Actions (read). - An outage stopped CI. On November 9 GitHub had an incident in which anonymous release downloads returned 504. Installing OpenTofu, the providers and cf-terraforming all failed, so our plans could not run for several hours. We waited instead of working around it.
What we would keep
- A zero-change plan is the definition of a correct import. It turned every mismatch into something concrete to fix before anything was applied.
- Treat generated code as a draft. The generator saved typing, but each of its outputs had to be read, and three of them were wrong or missing.
- Decide ownership before importing. Leaving Workers to their own repositories avoided two tools fighting over the same routes.
- The approval should show the exact plan. The approver reads the saved plan in the job summary, and that file is what gets applied.
Rules we follow now
- New infrastructure, including a new repository, starts as a pull request to the infrastructure repository, not as a click in a dashboard.
- Retiring something is a pull request that removes its code; the plan shows what will be destroyed, and a repository is archived rather than deleted.
- A read-only workflow runs a plan every Monday. A difference fails it, which tells us something was changed outside the code.
- The API token and the App get a new permission only when something they manage needs it.
Next steps
The first weekly drift check ran on November 9 and found no difference. We will see what the weekly runs report over the coming weeks, and decide then whether to manage more (for example the Worker routes) once the current split has been tested by real changes.
References
Our own records (private repositories, named here so the claims above can be traced):
- ziv-labs/agentic-factory-vnext #296–#302 — the plan and its six steps, with the owner's decisions
- ziv-labs/infra PR #2, #3 — the read-only inventory and how it was sorted
- ziv-labs/infra PR #4, #5 — running cf-terraforming in CI (the CI variable, the scratch directory)
- ziv-labs/infra PR #6, #7 — zone, DNS, R2 and Web Analytics: 15 to import, 0 to change; the R2 lifecycle written by hand
- ziv-labs/infra PR #8 — Pages, its domain and D1: 3 imported, 0 changed
- ziv-labs/infra PR #10, #12 — GitHub: 119 imported, 10 changed in state only; branch rules on, 5 added, 5 changed
- ziv-labs/infra PR #13, #14 — the operating rules and the weekly drift check
Related notes
- OAuth grants and leaked tokens — Vercel's April 2026 bulletin on an incident via an AI tool's OAuth app, and local tests of what limits a leaked grant: PKCE, redirect URIs, scopes, audiences.