Terraform interview question · Most asked
Walk through terraform init, plan and apply. What does each do?
Summary
init configures the backend and downloads providers and modules (recording provider versions in .terraform.lock.hcl); plan refreshes the state from real infrastructure, compares it with the configuration and shows the proposed changes; apply executes a plan (ideally a saved plan file reviewed earlier) and updates the state.
Longer Answer
In CI/CD, run terraform plan -out=tfplan on pull requests, post the plan for review, and apply exactly that saved plan after approval, so what was reviewed is what runs. terraform fmt -check and terraform validate catch formatting and syntax errors early. destroy removes everything in the configuration and should be heavily restricted in production.
Read plans carefully for -/+ (destroy and recreate) and - (destroy) actions: a renamed resource or a changed immutable attribute can replace a database.
Key Points
- init: backend, providers, modules, lock file.
- plan: refresh + diff.
- apply: execute (a saved plan).
- Watch for replacements in plans.
More most-asked Terraform questions
- How do you handle secrets in Terraform?
- A terraform plan wants to destroy and recreate a database. What do you do?
- What is Terraform, and how does it work at a high level?
- What is Terraform state, why is it needed, and where should it be stored?
- What are Terraform modules, and how do you design good ones?
- How would you run Terraform in CI/CD?
- count or for_each: when do you use each?
- How do you bring existing infrastructure under Terraform management?