Commit terraform.tfstate to the Repo So Everyone Shares It?
A teammate wants to commit terraform.tfstate to git so the whole team shares one state file. Defend the correct way to share Terraform state.
Answer key reviewed by Branko Petric
Read first: Terraform State Explained
Your decision
Two engineers ran terraform apply from their laptops and got conflicting results. A teammate proposes committing terraform.tfstate to the git repo so everyone shares the same state. Do you agree, and what do you do instead?
The situation
Your team manages infrastructure with Terraform. Two engineers ran terraform apply from their own laptops against the same environment and ended up with divergent state and a resource that exists in the cloud but not in either local state file. Applies are now producing confusing plans that want to create things that already exist.
Context
Each engineer has their own local terraform.tfstate on disk. There is no shared backend configured. The Terraform configuration provisions an RDS database, so the state includes the generated master password in plaintext. A teammate suggests committing terraform.tfstate to the git repository so everyone pulls the same state before running apply.
Worked interview answers
- DevOps Take Home Assignment: What Reviewers Grade and How to Pass
How to pass a DevOps take home assignment: common task types, what reviewers grade, how to scope the time, a README template, and the review call questions.
- Terraform Scenario Based Interview Questions: Read the Plan, Then Answer
Terraform scenario based interview questions, worked: a destroy in the plan, state drift, import vs recreate, state in git and blast radius, with real commands.
Related challenges
- Terraform Doesn't Know the Hand-Made DB. Let It Build a New One?
A prod RDS instance was made by hand and Terraform's plan wants to create a new one. A teammate says just apply. Defend what you actually do first.
- Terraform Wants to Replace the Database. Apply Now?
A routine terraform plan unexpectedly wants to destroy and recreate the production database. Defend whether to apply, and how.
- Someone Changed Infra by Hand. Terraform Plan Is a Mess.
A manual console change put Terraform's state out of sync, and the next plan wants to undo it. Defend how you reconcile it.
- One Giant Terraform State Runs Everything
A single Terraform state holds all your infra; every plan is slow and scary. A teammate wants to just keep applying carefully. Defend how you reduce the blast radius.