JuniorIaC & Automation

What is Terraform state, and why does it exist?

What they are really testing: Whether you understand state as the mapping between config and real resources that Terraform uses to compute diffs, and that it can contain sensitive data.

A real interview question

What is Terraform state, and why does it exist?

What most people say

drag me

State is a file Terraform uses to keep track of things.

Too vague. The point is that state maps your config to real resources so Terraform can compute diffs, and that it can hold sensitive data, which drives how you must store it. "Keeps track of things" misses both.

The follow-ups they ask next

  • Why can you not just commit the state file to git?

    It often contains secrets in plaintext, and committing it (especially to a shared/public repo) leaks them. It also has no locking in git, so concurrent edits corrupt it. Use a secured remote backend instead.

  • What happens if the state file is lost?

    Terraform loses track of the resources it manages, so it may try to recreate things that already exist or be unable to update/destroy them. You would have to import resources back into a new state, which is painful, hence protecting/backing up state.

What the interviewer is listening for

  • State maps config to real resources for diffs
  • Knows why diffs need it
  • Knows it holds secrets / must be secured

What sinks the answer

  • "Keeps track of things" only
  • Unaware it can contain secrets
  • No notion of why diffs need state

If you genuinely do not know

Say this instead of freezing. Reasoning out loud from what you do know beats silence every single time, and a good interviewer is listening for exactly that.

State is [a file mapping your config to the real resources Terraform created]. It exists so Terraform can [compute the diff on the next run, what to add/change/destroy, without rediscovering everything]. It stores [resource IDs/attributes/dependencies] for accurate plans. It [can contain secrets in plaintext], so [store it in a secured remote backend, never commit it].

Keep going with iac & automation

All 336 cloud engineer questions

Knowing the answer is not the same as recalling it under pressure

Sign in to send the questions you fumble to spaced recall, so they come back right before you would forget them, and learn the concepts behind them with hands-on labs.

Start free