What is Terraform state, and why must it be stored remotely?
IntermediateConceptInfrastructure & cloud basics
Answer
In one line: Terraform state is a file (terraform.tfstate) where Terraform records which real resources it manages and their current details; in teams it must be stored remotely with locking so everyone works from the same, safe, up-to-date copy.
What state is used for:
- Mapping — links
aws_instance.webin your code to the real instance IDi-0abc123. - Planning — Terraform compares your code, the state and the real world to work out what to change.
- Dependencies and metadata — tracks resource relationships and outputs.
- Performance — avoids querying every resource every time in large setups.
Why local state is a problem in a team:
- Each engineer has a different copy — someone applies with an old copy and Terraform tries to recreate or destroy things.
- No locking — two people apply at the same time and corrupt the state.
- Lost laptop = lost state — Terraform no longer knows what it manages.
- Secrets in state — state can contain passwords and keys in plain text; it shouldn't sit on laptops or in Git.
Remote state solves this:
terraform {
backend "s3" {
bucket = "acme-terraform-state"
key = "prod/network/terraform.tfstate"
region = "ap-south-1"
encrypt = true
use_lockfile = true # S3-native locking (older setups use a DynamoDB table)
}
}
Other backends: Azure Storage (blob lease locking), Google Cloud Storage, Terraform Cloud/HCP Terraform, GitLab-managed state.
Good practices:
- Encrypt the state bucket and restrict access tightly.
- Enable versioning on the bucket so you can recover a broken state.
- Separate state per environment and component (network, cluster, app) to reduce blast radius.
- Never edit state by hand; use
terraform state mv,rm,importormoved/importblocks. - Never commit state to Git — add
*.tfstateto.gitignore. - Apply from a pipeline, not from laptops.
Tip for interviews: mention locking, encryption, versioning and splitting state — that covers every common follow-up.
More on Infrastructure & cloud basics
What is the difference between declarative and imperative infrastructure?BeginnerWhat is idempotency, and why is it important in automation?BeginnerWhat is configuration drift, and how do you prevent it?BeginnerWhat is immutable infrastructure?BeginnerWhat does "pets vs cattle" mean?BeginnerWhat is cloud computing, and what are IaaS, PaaS and SaaS?Beginner