Terraform interview question · Most asked
What is Terraform state, why is it needed, and where should it be stored?
Summary
State maps resources in your configuration to real objects (IDs, attributes), records dependencies and metadata, and lets Terraform compute plans quickly. For teams it must be stored remotely in a shared backend with locking (for example S3, Azure Blob Storage, Google Cloud Storage, or HCP Terraform), never committed to Git.
Longer Answer
Why remote: everyone and every pipeline must see the same state; locking stops two applies from running at once and corrupting it; and the backend can be versioned and encrypted. State often contains secrets (database passwords, generated keys) in plain text, so restrict access, encrypt at rest and enable versioning so you can recover from mistakes.
With the S3 backend, recent Terraform versions support native lock files in S3 (use_lockfile = true), replacing the older DynamoDB lock table.
Code Example
terraform {
backend "s3" {
bucket = "acme-terraform-state"
key = "prod/network/terraform.tfstate"
region = "eu-west-1"
encrypt = true
use_lockfile = true
}
}
Key Points
- Maps config to real resource IDs.
- Remote backend with locking and versioning.
- State can contain secrets: protect it.
- Never commit state to Git.
Explanation
What interviewers look for
Understanding of why state exists, locking, and that state is sensitive.
Follow-up questions
- What happens if two engineers apply at the same time without locking?
- How would you recover a corrupted state file?
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?
- Walk through terraform init, plan and apply. What does each do?
- 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?