State Commands
The terraform state subcommands let you inspect and surgically edit tracked resources.
The terraform state subcommands let you inspect and manipulate what Terraform tracks, without touching your configuration.
Common subcommands
state list— list every tracked resource.state show ADDRESS— print one resource's attributes.state mv OLD NEW— rename or move a resource in state (for refactors).state rm ADDRESS— stop managing a resource without destroying it.
Example
# List everything Terraform manages
terraform state list
# Rename a resource after refactoring code
terraform state mv aws_instance.web aws_instance.frontend
# Forget a resource without destroying it
terraform state rm aws_s3_bucket.legacyWhen to use it
- An engineer uses terraform state mv to rename a resource in state after refactoring its HCL name, avoiding an unnecessary destroy-and-recreate.
- A team member uses terraform state rm to remove a resource from tracking after the underlying cloud resource was deleted manually, preventing errors on the next plan.
- An SRE uses terraform state pull to download the current remote state as JSON and inspect actual attribute values before making a sensitive change.
More examples
Move resource in state
Renames a resource's state key to match the updated HCL block name so Terraform does not destroy and recreate the instance.
# Rename a resource without destroying it
# Old config: resource "aws_instance" "server"
# New config: resource "aws_instance" "web_server"
terraform state mv \
aws_instance.server \
aws_instance.web_server
# Verify no planned changes remain
terraform planRemove resource from state
Deregisters a resource from Terraform state after it was deleted outside of Terraform, preventing plan errors.
# Resource was deleted manually; remove from state
terraform state rm aws_s3_bucket.old_data
# Confirm it is no longer tracked
terraform state list | grep old_data
# (no output)Pull and inspect remote state
Pulls the current state from the remote backend and uses jq to extract a specific resource attribute for inspection.
# Download state as JSON for inspection
terraform state pull > current_state.json
# Find a specific attribute
jq '.resources[] | select(.name=="web") | .instances[0].attributes.public_ip' \
current_state.json
Discussion