Terraform state is the single most important file in your infrastructure codebase. It maps your HCL configuration to real-world resources. Lose it, and Terraform doesn't know what exists.
What Is State?
The state file (terraform.tfstate) records:
- Which resources Terraform manages
- Resource IDs, ARNs, and attributes
- Dependencies between resources
- Output values
# View current state
terraform state list
terraform state show aws_instance.webRemote State Backend
Never use local state for team projects. Set up a remote backend:
S3 + DynamoDB (AWS)
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "production/terraform.tfstate"
region = "eu-west-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}Create the backend resources first:
# backend-setup/main.tf (run once, locally)
resource "aws_s3_bucket" "state" {
bucket = "my-terraform-state"
}
resource "aws_s3_bucket_versioning" "state" {
bucket = aws_s3_bucket.state.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "state" {
bucket = aws_s3_bucket.state.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
}
}
}
resource "aws_s3_bucket_public_access_block" "state" {
bucket = aws_s3_bucket.state.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_dynamodb_table" "locks" {
name = "terraform-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}Terraform Cloud
terraform {
cloud {
organization = "my-org"
workspaces {
name = "production"
}
}
}Master this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →State Locking
Prevents two people from modifying state simultaneously:
Alice: terraform apply → Acquires lock → Makes changes → Releases lock
Bob: terraform apply → Waits for lock → "Error: state locked by Alice"DynamoDB provides locking for S3 backends. Terraform Cloud has built-in locking.
Force unlock (emergency only):
terraform force-unlock LOCK_IDState Operations
Import Existing Resources
Bring manually-created resources under Terraform management:
# Define the resource in HCL first
resource "aws_instance" "legacy_server" {
ami = "ami-0c1c30571d2dae5c9"
instance_type = "t3.micro"
}# Import by resource ID
terraform import aws_instance.legacy_server i-0abc123def456
# Verify
terraform plan # Should show no changesMove Resources
Rename without destroying:
# Rename a resource
terraform state mv aws_instance.old_name aws_instance.new_name
# Move into a module
terraform state mv aws_instance.web module.compute.aws_instance.web
# Move between state files
terraform state mv -state-out=other.tfstate aws_instance.web aws_instance.webOr use moved blocks (preferred in Terraform 1.1+):
moved {
from = aws_instance.old_name
to = aws_instance.new_name
}Remove from State
Stop managing a resource without destroying it:
terraform state rm aws_instance.legacy
# Resource still exists in AWS, but Terraform forgets about itState Isolation Patterns
Per-Environment State
environments/
dev/
main.tf
backend.tf → s3://state/dev/terraform.tfstate
staging/
main.tf
backend.tf → s3://state/staging/terraform.tfstate
production/
main.tf
backend.tf → s3://state/production/terraform.tfstatePer-Component State
infrastructure/
network/
backend.tf → s3://state/network/terraform.tfstate
database/
backend.tf → s3://state/database/terraform.tfstate
application/
backend.tf → s3://state/application/terraform.tfstateReference outputs across states:
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = "my-terraform-state"
key = "network/terraform.tfstate"
region = "eu-west-1"
}
}
resource "aws_instance" "app" {
subnet_id = data.terraform_remote_state.network.outputs.private_subnet_id
}Get weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Disaster Recovery
S3 Versioning
With versioning enabled, recover from accidental state corruption:
# List state versions
aws s3api list-object-versions --bucket my-terraform-state --prefix production/terraform.tfstate
# Restore a previous version
aws s3api get-object --bucket my-terraform-state \
--key production/terraform.tfstate \
--version-id "VERSION_ID" \
restored-state.tfstateState Backup Before Risky Operations
# Manual backup
terraform state pull > backup-$(date +%Y%m%d).tfstate
# Restore from backup
terraform state push backup-20260301.tfstateCommon State Problems
| Problem | Solution |
|---|---|
| State locked after crash | terraform force-unlock LOCK_ID |
| State out of sync | terraform refresh (or terraform apply -refresh-only) |
| Resource exists but not in state | terraform import |
| Want to stop managing a resource | terraform state rm |
| Need to rename without recreate | moved block or terraform state mv |
| State file corruption | Restore from S3 version history |
What's Next?
Our Terraform for Beginners course covers state management in depth across 15 hands-on lessons — remote backends, locking, imports, and multi-environment patterns. First lesson is free.
---
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
GitHub Actions CI/CD for Terraform
Automate Terraform with GitHub Actions. Plan on PR, apply on merge, remote state locking, and secure secrets for IaC pipelines.
Terraform Workspaces for Environments
Use Terraform workspaces for dev, staging, and production. Practical patterns, trade-offs, and best practices for multi-env IaC.
Terraform AWS Exercises for Practice
Hands-on Terraform AWS exercises from beginner to advanced. Practice EC2, S3, VPC, RDS provisioning with real infrastructure.
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.
Terraform vs CloudFormation vs Pulumi
Compare the top Infrastructure as Code tools — Terraform, AWS CloudFormation, and Pulumi. Understand their strengths, weaknesses, and ideal use cases.
Explore topics
Browse more articles on the topics covered here.