New batchZero to DevOps starts 29 Nov · limited seatsReserve your seat
Cheat sheetsTerraformTerraform
FreeSheet 06 of 9

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

Lists all resources currently tracked in the state file.

Basic Usage

bash
$ terraform state list
aws_instance.example
aws_vpc.main
aws_subnet.public
aws_security_group.web

Filter by Resource Name

bash
$ terraform state list aws_instance.example
aws_instance.example

List with Module Resources

bash
$ 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

bash
$ 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

bash
$ terraform state show 'module.vpc.aws_subnet.this[0]'

Show Specific Attribute (via grep)

bash
$ 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

bash
$ 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

bash
$ terraform state mv aws_instance.example module.ec2.aws_instance.example

Move Module Resource Out

bash
$ terraform state mv 'module.vpc.aws_subnet.this[0]' aws_subnet.public

Move Multiple Resources with -dry-run

bash
$ 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

bash
$ terraform state mv -force aws_instance.example aws_instance.old_example

terraform state rm — Remove Resource from State

Removes a resource from the state file WITHOUT destroying it.

Basic Usage

bash
$ terraform state rm aws_instance.example
Removed aws_instance.example
Successfully removed 1 resource instance(s).

Remove Multiple Resources

bash
$ terraform state rm aws_instance.example aws_instance.old_example

Remove All Resources ( DANGEROUS)

bash
$ terraform state rm $(terraform state list)

Remove with -dry-run

bash
$ 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

bash
$ terraform state pull
{
  "version": 4,
  "terraform_version": "1.6.0",
  "serial": 5,
  "lineage": "abc-123-456",
  "outputs": {},
  "resources": [...]
}

Save State to File

bash
$ terraform state pull > state-backup.json

Pretty Print State

bash
$ terraform state pull | jq '.'

terraform state push — Push State

Pushes a state file to the remote backend (overwrites current state).

Basic Usage

bash
$ terraform state push state-backup.json

** DANGEROUS** — Overwrites remote state. Use only when necessary.

Force Push (Skip Safety Checks)

bash
$ terraform state push -force state-backup.json

terraform state replace-provider — Replace Provider

Replaces the provider for a resource in the state (useful when migrating providers).

Basic Usage

bash
$ terraform state replace-provider registry.terraform.io/-/aws registry.terraform.io/hashicorp/aws
Replacing provider registry.terraform.io/-/aws with registry.terraform.io/hashicorp/aws

Replace Specific Provider Version

bash
$ terraform state replace-provider registry.terraform.io/hashicorp/aws=5.0.0 registry.terraform.io/hashicorp/aws=5.48.0

terraform taint — Mark Resource for Recreation

Marks a resource as tainted, forcing recreation on the next apply.

Basic Usage

bash
$ terraform taint aws_instance.example
Resource instance aws_instance.example has been marked as tainted.

Next terraform apply will destroy and recreate the resource.

Taint Module Resource

bash
$ terraform taint 'module.vpc.aws_subnet.this[0]'

terraform untaint — Clear Taint

Removes the taint from a resource, preventing forced recreation.

Basic Usage

bash
$ terraform untaint aws_instance.example
Resource instance aws_instance.example has been successfully untainted.

Quick Reference — Most Used Commands

TaskCommand
List all resources in stateterraform state list
Show resource detailsterraform state show aws_instance.example
Move resource in stateterraform state mv aws_instance.example aws_instance.old
Remove resource from stateterraform state rm aws_instance.example
Pull state to fileterraform state pull > state.json
Push state from fileterraform state push state.json
Replace providerterraform state replace-provider old new
Mark resource for recreationterraform taint aws_instance.example
Clear taintterraform untaint aws_instance.example

Pro Tips

  1. Always take a state backup before destructive state operations:

    bash
    terraform state pull > backup-state.json
  2. Use state mv when refactoring to avoid destroying and recreating resources.

  3. Use terraform state rm only when resource was manually deleted outside Terraform.

  4. Taint vs Replace — Use taint for manual recreation, use -replace flag with apply for controlled recreation:

    bash
    terraform apply -replace="aws_instance.example"
  5. Never edit .tfstate directly — use state subcommands instead.

  6. State locking — some commands may fail if state is locked. Use terraform force-unlock (Part 5).

  7. Pull before pushing — avoid overwriting remote state with stale local state.

  8. Module resource addressing — use quotes to handle special characters:

    bash
    terraform state show 'module.vpc.aws_subnet.this[0]'

Next in TerraformTesting & Configuration Quality Pro
Terraform cheat sheet: State Management | InfraThrone DevOps Park