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

← All Terraform questions