State Management
A comprehensive developer cheatsheet covering Terraform state management, featuring essential CLI commands and operational workflows for inspecting tracked resources (list, show), refactoring infrastructure (mv, rm), synchronizing remote backend files (pull, push), and controlling resource recreation (taint, untaint).
Table of Contents
- terraform state list — List Resources in State
- terraform state show — Show Resource Details
- terraform state mv — Move Resource in State
- terraform state rm — Remove Resource from State
- terraform state pull — Pull Current State
- terraform state push — Push State
- terraform state replace-provider — Replace Provider
- terraform taint — Mark Resource for Recreation
- terraform untaint — Clear Taint
- Quick Reference — Most Used Commands
- Pro Tips
terraform state list — List Resources in State
Lists all resources currently tracked in the state file.
Basic Usage
$ terraform state list
aws_instance.example
aws_vpc.main
aws_subnet.public
aws_security_group.webFilter by Resource Name
$ terraform state list aws_instance.example
aws_instance.exampleList with Module Resources
$ terraform state list
module.vpc.aws_vpc.this
module.vpc.aws_subnet.private[0]
module.vpc.aws_subnet.private[1]terraform state show — Show Resource Details
Displays detailed attributes of a specific resource in the state.
Basic Usage
$ terraform state show aws_instance.example
# aws_instance.example:
resource "aws_instance" "example" {
ami = "ami-0abcdef1234567890"
instance_type = "t2.micro"
id = "i-0dae5d4daa47fe4a2"
public_ip = "54.123.45.67"
private_ip = "10.0.1.100"
tags = {
"Name" = "example-instance"
}
}Show Module Resource
$ terraform state show 'module.vpc.aws_subnet.this[0]'Show Specific Attribute (via grep)
$ terraform state show aws_instance.example | grep public_ip
public_ip = "54.123.45.67"terraform state mv — Move Resource in State
Moves a resource to a different address in the state (useful for refactoring).
Basic Usage
$ terraform state mv aws_instance.example aws_instance.old_example
Move "aws_instance.example" to "aws_instance.old_example"
Successfully moved 1 resource(s).Move Resource into Module
$ terraform state mv aws_instance.example module.ec2.aws_instance.exampleMove Module Resource Out
$ terraform state mv 'module.vpc.aws_subnet.this[0]' aws_subnet.publicMove Multiple Resources with -dry-run
$ terraform state mv -dry-run aws_instance.example aws_instance.old_example
Would move "aws_instance.example" to "aws_instance.old_example"Force Move Even if Destination Exists
$ terraform state mv -force aws_instance.example aws_instance.old_exampleterraform state rm — Remove Resource from State
Removes a resource from the state file WITHOUT destroying it.
Basic Usage
$ terraform state rm aws_instance.example
Removed aws_instance.example
Successfully removed 1 resource instance(s).Remove Multiple Resources
$ terraform state rm aws_instance.example aws_instance.old_exampleRemove All Resources ( DANGEROUS)
$ terraform state rm $(terraform state list)Remove with -dry-run
$ terraform state rm -dry-run aws_instance.example
Would remove "aws_instance.example"terraform state pull — Pull Current State
Pulls the current state from the backend and outputs it.
Basic Usage
$ terraform state pull
{
"version": 4,
"terraform_version": "1.6.0",
"serial": 5,
"lineage": "abc-123-456",
"outputs": {},
"resources": [...]
}Save State to File
$ terraform state pull > state-backup.jsonPretty Print State
$ terraform state pull | jq '.'terraform state push — Push State
Pushes a state file to the remote backend (overwrites current state).
Basic Usage
$ terraform state push state-backup.json** DANGEROUS** — Overwrites remote state. Use only when necessary.
Force Push (Skip Safety Checks)
$ terraform state push -force state-backup.jsonterraform state replace-provider — Replace Provider
Replaces the provider for a resource in the state (useful when migrating providers).
Basic Usage
$ terraform state replace-provider registry.terraform.io/-/aws registry.terraform.io/hashicorp/aws
Replacing provider registry.terraform.io/-/aws with registry.terraform.io/hashicorp/awsReplace Specific Provider Version
$ terraform state replace-provider registry.terraform.io/hashicorp/aws=5.0.0 registry.terraform.io/hashicorp/aws=5.48.0terraform taint — Mark Resource for Recreation
Marks a resource as tainted, forcing recreation on the next apply.
Basic Usage
$ terraform taint aws_instance.example
Resource instance aws_instance.example has been marked as tainted.Next
terraform applywill destroy and recreate the resource.
Taint Module Resource
$ terraform taint 'module.vpc.aws_subnet.this[0]'terraform untaint — Clear Taint
Removes the taint from a resource, preventing forced recreation.
Basic Usage
$ terraform untaint aws_instance.example
Resource instance aws_instance.example has been successfully untainted.Quick Reference — Most Used Commands
| Task | Command |
|---|---|
| List all resources in state | terraform state list |
| Show resource details | terraform state show aws_instance.example |
| Move resource in state | terraform state mv aws_instance.example aws_instance.old |
| Remove resource from state | terraform state rm aws_instance.example |
| Pull state to file | terraform state pull > state.json |
| Push state from file | terraform state push state.json |
| Replace provider | terraform state replace-provider old new |
| Mark resource for recreation | terraform taint aws_instance.example |
| Clear taint | terraform untaint aws_instance.example |
Pro Tips
-
Always take a state backup before destructive state operations:
bashterraform state pull > backup-state.json -
Use
state mvwhen refactoring to avoid destroying and recreating resources. -
Use
terraform state rmonly when resource was manually deleted outside Terraform. -
Taint vs Replace — Use
taintfor manual recreation, use-replaceflag withapplyfor controlled recreation:bashterraform apply -replace="aws_instance.example" -
Never edit
.tfstatedirectly — usestatesubcommands instead. -
State locking — some commands may fail if state is locked. Use
terraform force-unlock(Part 5). -
Pull before pushing — avoid overwriting remote state with stale local state.
-
Module resource addressing — use quotes to handle special characters:
bashterraform state show 'module.vpc.aws_subnet.this[0]'