What Is Terraform State?
Terraform state is a JSON file (terraform.tfstate) that maps your configuration to real-world resources. It's how Terraform knows what exists, what changed, and what needs updating.
Why State Matters
Without state, Terraform would:
- Not know which resources it manages
- Recreate everything on every apply
- Have no way to detect drift
- Be unable to handle dependencies
Master this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →State Commands
# List resources in state
terraform state list
# Show details of a resource
terraform state show aws_instance.web
# Remove a resource from state (without destroying it)
terraform state rm aws_instance.legacy
# Move/rename a resource in state
terraform state mv aws_instance.old aws_instance.new
# Pull remote state locally
terraform state pullRemote State
Never store state locally in a team. Use a remote backend:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "eu-west-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}State Locking
Prevents two people from modifying state simultaneously:
# DynamoDB table for locking (with S3 backend)
resource "aws_dynamodb_table" "terraform_locks" {
name = "terraform-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}Get weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Best Practices
- Always use remote state for team projects
- Enable encryption — state contains sensitive data
- Enable locking — prevent concurrent modifications
- Don't edit state manually — use
terraform statecommands - Use workspaces for environment separation
- Back up state — enable versioning on your S3 bucket
Sensitive Data in State
State can contain passwords, keys, and secrets. Protect it:
output "db_password" {
value = aws_db_instance.main.password
sensitive = true
}Learn More
Master state management hands-on in our Terraform for Beginners course.
---
Ready to go deeper? Check out our hands-on course: Terraform for Beginners — practical exercises you can follow along on your own machine.
Ready to learn by doing?
Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.
Related Articles
Terraform Expert Tips and Patterns
Level up Terraform skills with expert patterns. Dynamic blocks, for_each, moved blocks, validation, and production workflows.
Terraform Modules Best Practices
Write reusable Terraform modules. Structure, variables, outputs, semantic versioning, automated testing, and registry publishing.
Tofu vs Terraform Comparison
OpenTofu forked Terraform after the BSL license change. Compare features, compatibility, licensing, and ecosystem to decide which IaC tool fits your team.
Terraform State Management Guide
Master Terraform state management. Remote backends, state locking, import commands, state moves, and disaster recovery procedures.
Terraform Testing with Terratest
Terratest lets you write automated tests for Terraform infrastructure using Go. Learn how to test Terraform modules, validate cloud resources, and integrate.
Terraform Variables and Outputs
Terraform variables and outputs. Types, validation rules, locals, sensitive values, tfvars files, and cross-module dependencies.
Explore topics
Browse more articles on the topics covered here.