Terraform State Management and Why Remote State Matters
Fast recall
Quick revision
Core answer
Whether you understand the risks of local state in team environments and can configure remote state with locking correctly.
Using local state in a team environment, leading to state drift and concurrent modification corruption, or storing state in Git where secrets in state files become a security issue.
Explain state locking mechanism (DynamoDB for S3 backend), state file encryption at rest, and the use case for `terraform state` subcommands for surgical state manipulation.
Interview-ready guide
Interview answer
Terraform state is the source of truth that maps your HCL declarations to real cloud resource IDs. Storing it locally is only appropriate for single-developer experiments; any team workflow requires remote state stored in a shared backend.
Why remote state is mandatory for teams
- Local
terraform.tfstatefiles on individual machines lead to state conflicts: two engineers apply concurrently and each writes a different state file; one overwrites the other's changes, and Terraform no longer knows the real infrastructure state - Remote backends (S3, GCS, Terraform Cloud) store state centrally with state locking — while one engineer is running
apply, any otherplanorapplywaits; the lock prevents concurrent modifications that corrupt state - Configure S3 remote state with DynamoDB locking:
backend "s3" { bucket = "my-tfstate"; key = "prod/app/terraform.tfstate"; region = "us-east-1"; dynamodb_table = "terraform-locks"; encrypt = true }— encryption at rest is mandatory because state files contain database passwords and private keys in plaintext
`terraform_remote_state` data source
- The networking module creates a VPC and exports the VPC ID in an
output; the application module reads it usingdata "terraform_remote_state" "network"— this decouples module deployment without hardcoding IDs - Cross-stack references via remote state avoid the brittle pattern of copying output values between configurations manually; the application module always reads the current VPC ID, even after the network team replaces it
- Restrict read access to remote state that contains sensitive outputs — any module reading the networking state can see all its outputs, including sensitive ones; use separate state files for secrets-heavy infrastructure
State file safety
- Never delete state file entries manually — use
terraform state rmto remove a resource from state without destroying the real resource; direct JSON edits corrupt the state format - Back up state before destructive operations:
terraform state pull > backup.tfstate; S3 versioning on the state bucket provides automatic backups that you can restore withaws s3 cp
Terraform State Management and Why Remote State Matters
Direct Answer
Terraform state maps config to real resources; remote state (S3 + DynamoDB) is essential in teams to enable collaboration, prevent concurrent modification via locking, and securely store sensitive values.
Code Example
# Remote state backend: S3 + DynamoDB for AWS
terraform {
backend "s3" {
bucket = "my-terraform-state-prod"
key = "services/payment-api/terraform.tfstate"
region = "us-east-1"
encrypt = true # AES-256 encryption at rest
dynamodb_table = "terraform-state-lock" # locking — prevents concurrent apply
}
}
# DynamoDB table for locking (created separately, once per account)
# resource "aws_dynamodb_table" "terraform_locks" {
# name = "terraform-state-lock"
# billing_mode = "PAY_PER_REQUEST"
# hash_key = "LockID"
# attribute {
# name = "LockID"
# type = "S"
# }
# }
# Sharing state between configurations
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = "my-terraform-state-prod"
key = "network/terraform.tfstate"
region = "us-east-1"
}
}
# Use output from the network stack
resource "aws_instance" "app" {
subnet_id = data.terraform_remote_state.network.outputs.private_subnet_id
}
Common Mistakes & Pitfalls
Using local state in a team environment, leading to state drift and concurrent modification corruption, or storing state in Git where secrets in state files become a security issue.
Check your understanding
Answer these without looking back at the article.
- How do you share state between separate Terraform configurations?
- What happens if the DynamoDB lock is left behind after a crash?
- How do you migrate from local state to remote state?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.