Infrastructure change control
Terraform in a running environment
In short: I brought existing infrastructure under management as code without rebuilding the services that run on it. No-change plans confirmed the configuration matched, and separate tests confirmed the state migration process.
01 / For your organisation
An environment that has run for years, configured by hand or with a mix of tools, can be brought under management as code without a rebuild. It gains change control, review and configuration history, while the working configuration is preserved.
The first change is possible only once the code describes the current state exactly, and Terraform state has locking, backups and a tested way back. Adoption does not require rebuilding the services.
02 / Delivered in 4digital’s environment
This is how I adopted part of 4digital’s own environment, including DNS, Zero Trust access, code repositories and the secrets store, and moved the state of existing projects to a private service with full change history. The starting point: an empty backend does not mean the resources do not exist. The work rested on four principles:
- Import first, manage later — an adoption is complete only when
terraform planshows “No changes”. - One owner for every object — Ansible owns hosts and system services; Terraform owns everything behind a service API. The handover point is explicit.
- Read-only import — in an isolated environment, access to the service went through a bridge that allowed only GET requests, so adoption could not affect how it runs.
- Migration with a way back — every state file copied byte for byte under a lock, with lineage and serial verified, a copy in a protected snapshot store and a lock on the old location that stops writes from the old configuration.
I built a private Terraform state service behind a Zero Trust access layer. Each state has its own object that serialises writes and holds a durable lock with no automatic expiry. Before every change, the previous and new state versions are archived as content-addressed snapshots protected from overwrite and deletion for 30 days; if a snapshot cannot be written, the state write is held back. Terraform connects through a short-lived local relay, so it never receives the storage key, and access secrets never end up in backend metadata or saved plans.
Restoring state requires the same lineage, checks the current hash and increments the serial. The tool refuses to delete state, change lineage or write a stale version, even with state push -force. The Terraform wrapper will not plan or apply until the state is entered in a reviewed catalogue of adopted states.
I adopted services one at a time: separate state for each, import, no-change plan and catalogue entry. The boundary between Ansible and Terraform moves in a single change: once an object is in Terraform, it leaves the Ansible configuration.
03 / Outcome
Selected running services have their configuration described and imported into Terraform. A no-change plan confirms the baseline. Adoption does not enable deployments automatically: the first modification requires a separate plan and approval. Passwords, tokens and repository content were deliberately left outside Terraform: the scope covers what is genuinely worth managing in code.
Value: change control, configuration history and repeatable infrastructure management — without having to replace an environment that already works.
04 / Selected evidence
- A no-change plan for every adopted service, confirmed over two consecutive cycles.
- State files moved byte for byte with change history preserved and a migration lock confirmed by trial.
- A live trial of the state service: no-change plan, competing lock, restore from snapshot and another no-change plan.
- No data changes after adoption: full repository integrity checks, matching artefacts, identities and permissions.
- Source of experience
- 4digital’s own environment
- Discipline
- Infrastructure as code
- Technology areas
- Terraform · Ansible · Cloudflare Access, Workers, Durable Objects and R2 · Vault · sops · Forgejo · State migration