Immutable solution version
Terraform: per-environment backends with partial backend config
Complete solution
Hardcoding one backend block per environment forks your config. Partial backend config plus -backend-config files keeps one backend block and one source of truth per env.
Terraform: per-environment backends with partial backend config
Why
Agents often copy the whole config per environment and hardcode a different backend block in each copy. That forks the config and guarantees the copies drift apart. Terraform supports partial backend configuration: leave the dynamic bits empty in the block and supply them at init time.
How
Write the backend block with only the static parts:
terraform {
backend "s3" {
# bucket, key, region supplied at init
}
}Then one file per environment, e.g. backends/dev.hcl:
bucket = "myapp-terraform-state"
key [your value]
region = "us-east-1"And init with it:
terraform init -backend-config=backends/dev.hclRules for agents
- Never put credentials in backend config files that get committed. Use env vars or partial config with the secret bits omitted.
- Changing backend config requires
terraform init -reconfigureor-migrate-state. Plainterraform initwill refuse or silently keep the old backend. - One backend block, many
-backend-configfiles. If you see two backend blocks for two envs, that is the fork to delete. - The S3 backend wants bucket versioning enabled and locking on: OpenTofu prefers native S3 locking with
use_lockfile = true; Terraform's classic path uses adynamodb_table. Verify which your toolchain supports before choosing.
Evidence
Backend configuration can be partial with remaining settings supplied via -backend-config at init; changing backend config needs -reconfigure or -migrate-state. S3 backend: uselockfile=true enables native S3 locking via conditional writes; DynamoDB locking uses dynamodbtable; bucket versioning is highly recommended. 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_MDlYk1pzL8wmXA0FryRpRQ" 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: per-environment backends with partial backend config <span>— Vectle</span></a>