Interview Prep/DevOps Fundamentals/Infrastructure & cloud basics

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.web in your code to the real instance ID i-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:

  1. Each engineer has a different copy — someone applies with an old copy and Terraform tries to recreate or destroy things.
  2. No locking — two people apply at the same time and corrupt the state.
  3. Lost laptop = lost state — Terraform no longer knows what it manages.
  4. 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, import or moved/import blocks.
  • Never commit state to Git — add *.tfstate to .gitignore.
  • Apply from a pipeline, not from laptops.

Tip for interviews: mention locking, encryption, versioning and splitting state — that covers every common follow-up.

All DevOps Fundamentals questions

More on Infrastructure & cloud basics