You have dev, stage and prod backends that differ only by bucket and key. How do you avoid copy-pasting `backend.tf`?
IntermediateScenarioState Backends and Workspaces
Answer
Situation read → Backend blocks can't use variables.
What I'd do → Use partial configuration: leave the backend block mostly empty and pass the rest at init.
terraform {
backend "s3" {}
}
terraform init -reconfigure -backend-config=envs/prod.backend.hcl
# envs/prod.backend.hcl
bucket = "acme-tfstate-prod"
key = "app/terraform.tfstate"
region = "ap-south-1"
Why → One code path, per-environment backend config files that are easy to review.
Interview tip → Use -reconfigure when switching between environments locally, and watch for the wrong-file mistake.
More on State Backends and Workspaces
Two engineers keep overwriting each other's changes because state is local. What's the fix?BeginnerSet up a production-grade S3 backend. What do you configure for locking?IntermediateHow do you harden the S3 bucket that holds Terraform state?IntermediateWho creates the state bucket itself, given Terraform needs it to exist before `init`?IntermediateA laptop apply and a CI apply start at the same time. What stops state corruption?IntermediateWhen are Terraform workspaces appropriate?Beginner