Immutable solution version
Terraform workflow: migrate from workspaces to directory-per-environment
Complete solution
Workspaces served until prod needed real isolation. The migration: new directory roots per env, state moved per workspace, backend configs separated, CI retargeted.
Terraform workflow: migrate from workspaces to directory-per-environment
When
terraform workspace select nearly (or actually) applied dev config to prod state. The team wants hard separation: different backends, different credentials, no shared mutable selector.
Steps
- Create the directory structure:
envs/dev,envs/staging,envs/prod, each a root module calling the shared child modules. Extract the shared config into modules first if it is not already modular. - Per environment: copy the workspace's state to the new backend key. Procedure per env:
terraform workspace select [env],terraform state pull > backup-[env].json, configure the new directory's backend,terraform init -migrate-stateanswering with the backed-up state. - Verify per env:
terraform planin the new directory must be clean against that env's state. Do this for dev first, then staging, then prod. - Retarget CI: one job per directory, each with its own credentials and backend config. Delete the workspace-selection logic from the pipeline.
- Only after all envs verify: remove the old workspace states (
terraform workspace deleteafter selecting each, with the new dirs confirmed working).
Rules for agents
- One environment per session, lowest first. The prod migration happens last and gets the most review.
- Keep the shared modules identical across envs during migration. Refactoring modules AND migrating layout in one change is two risks multiplied.
- Different credentials per environment is the point of the exercise. If prod and dev still share credentials after migration, you moved deck chairs.
- After migration,
terraform workspacecommands should have no workspaces left with real state. Verify; leftover workspaces are confusion waiting to happen.
Evidence
Workspaces multiplex states from one config; init -migrate-state moves state to new backends; workspace delete removes empty workspaces. Verified against live docs 2026-09-26.
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_iDp_GfvQJEVsIqflFNO_Fw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Terraform workflow: migrate from workspaces to directory-per-environment <span>— Vectle</span></a>