Terraform state is where things go wrong. Here's how to manage it safely.
February 24, 2026 · 7 min · 1386 words · Rob Washington
Table of Contents
Terraform state is both essential and dangerous. It’s how Terraform knows what exists, what changed, and what to do. Mismanage it, and you’ll either destroy production or spend hours untangling drift.
# List all resourcesterraform state list
# Show specific resourceterraform state show aws_instance.web
# Pull remote state to local fileterraform state pull > state.json
# Moved resource in config from "old_name" to "new_name"terraform state mv aws_instance.old_name aws_instance.new_name
# Move to a moduleterraform state mv aws_instance.web module.compute.aws_instance.web
Remove resource from Terraform management (resource still exists):
1
2
3
4
5
6
7
# Stop managing this resourceterraform state rm aws_instance.legacy
# Useful when:# - Importing to different state# - Handing off to another team# - Resource should no longer be managed by Terraform
Bring existing infrastructure under Terraform management:
1
2
3
4
5
6
7
8
9
10
11
12
# Add resource block firstresource "aws_instance""imported"{# Will be filled in after import}# Import the resourceterraform import aws_instance.imported i-0abc123def456
# Run plan to see what attributes to addterraform plan
# Fill in the resource block to match reality
terraform workspace new staging
terraform workspace select production
Workspaces share the same backend config. They’re useful for lightweight environment separation but can get confusing. Many teams prefer separate directories.
Committing state to git: State contains secrets. Use remote backends.
No locking: Concurrent applies corrupt state. Always use locking.
Shared state for unrelated resources: Split state by environment and component.
Manual changes without updating state: Use -refresh-only or re-apply.
Force-unlocking carelessly: Make sure no one is actually running first.
No versioning on state bucket: You’ll regret this during recovery.
State management isn’t glamorous, but it’s where Terraform goes wrong. Set up remote state with locking from day one, enable versioning, and treat state operations with care. Your future self will thank you.
📬 Get the Newsletter
Weekly insights on DevOps, automation, and CLI mastery. No spam, unsubscribe anytime.