Interview › Infrastructure as Code (Terraform, Ansible)
What is the Terraform state file, and why is it critical?
Infrastructure as Code (Terraform, Ansible) · Basic level
Answer
The Terraform state file is Terraform's source of truth for mapping resource addresses in code to real provider object IDs. It is critical because Terraform cannot safely know what it manages, what changed, or what dependencies exist without state. Losing or corrupting state can lead to duplicates, orphaned resources, or destructive plans.
Technical explanation
State includes resource instance addresses, provider IDs, attributes, dependency metadata, and sometimes sensitive values.
State is not a cache that can be ignored; it is a transactional record Terraform uses to plan future changes.
State operations should be backed up, locked, encrypted, and tightly permissioned.
Keep Terraform's ownership boundary clear: one state should own a resource or field, and other tools should consume published outputs instead of modifying it.
Use fmt, validate, linting, policy checks, plan review, and state locking before production applies.
Design for small blast radius by splitting state around lifecycle, permissions, and recovery boundaries.
Hands-on example
1. Implement remote state and locking for: What is the Terraform state file, and why is it critical?
2. Create or use an S3 bucket with versioning, encryption, public access blocked, and least-privilege IAM. Then configure the backend:
terraform {
backend "s3" {
bucket = "company-tfstate-prod"
key = "platform/network/prod.tfstate"
region = "ap-south-1"
encrypt = true
use_lockfile = true
}
}
3. For legacy DynamoDB locking, verify the table has partition key LockID and migrate deliberately because newer S3 locking makes DynamoDB locking deprecated.
4. Open two terminals and start two applies against the same state key; confirm the second run waits or fails on the lock instead of writing concurrently.
5. Recover safely by checking the active process first; only use force-unlock when the original run is definitely dead.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- What is Infrastructure as Code, and what problems does it solve over click-ops?
- What is the difference between declarative and imperative IaC, and where do Terraform and Ansible fall?
- What is the difference between configuration management and provisioning?
- What is Terraform, and what is the core plan/apply workflow?
- What does terraform init do?
- Why should state be stored remotely, and what backend would you use on AWS?
- What is state locking, and why does it matter for teams?
- How does Terraform use DynamoDB for state locking with an S3 backend?