Skip to main content

State Commands

Imagine Terraform’s state file as the ledger of your Lego city. It records every building, road, and park you’ve created. Sometimes, you need to peek inside the ledger, move entries around, or fix mistakes.

That’s where state commands come in. They’re like librarian tools that let you inspect, list, and adjust Terraform’s memory without rewriting the entire blueprint. Used wisely, they keep your infrastructure consistent and save you from chaos.


Inspecting State

  • terraform state list → Lists all resources tracked in state.
  • terraform state show <resource> → Displays details of a specific resource.
  • Use case: Verify what Terraform thinks exists vs what’s actually deployed.

Moving Resources

  • terraform state mv <source> <destination> → Moves a resource within state.
  • Example: Renaming aws_instance.example to aws_instance.web.
  • Use case: Refactor configurations without recreating resources.

Removing Resources

  • terraform state rm <resource> → Removes a resource from state (but not from the cloud).
  • Use case: Stop Terraform from managing a resource while leaving it deployed.

Importing Resources

  • terraform import <resource> <id> → Adds an existing resource into state.
  • Example: Import an EC2 instance already created manually.
  • Use case: Bring “orphaned” resources under Terraform management.

Advanced Commands

  • terraform state pull → Fetches the current state file.
  • terraform state push → Uploads a modified state file (rarely used).
  • terraform force-unlock <LOCK_ID> → Releases a stuck lock (use cautiously).

Hands‑On Lab / Demo

Lab: Working with State Commands

  1. Provision an EC2 instance with Terraform.
  2. Run:
    • terraform state list → See all resources.
    • terraform state show aws_instance.example → Inspect details.
  3. Rename resource in config to aws_instance.web.
  4. Run:
    • terraform state mv aws_instance.example aws_instance.web → Move state entry.
  5. Manually create an S3 bucket in AWS console.
  6. Run:
    • terraform import aws_s3_bucket.mybucket mybucket → Import into state.

Pro Tips & Best Practices

  • Always back up state before running destructive commands.
  • Use state mv instead of recreating resources when refactoring.
  • Be cautious with state rm - Terraform will forget the resource but it still exists in the cloud.
  • Document imports so teammates know which resources are managed.
  • Avoid manual edits of state files - use commands instead.

Summary & Cheatsheet

  • Inspect: state list, state show.
  • Move: state mv.
  • Remove: state rm.
  • Import: terraform import.
  • Advanced: state pull, state push, force-unlock.
Quick mnemonic: List, Show, Move, Remove, Import

The Hackers Notebook

State commands are the surgical tools of Terraform. They let you inspect, adjust, and repair state without tearing down infrastructure. By mastering them, you gain control over Terraform’s memory and can handle real‑world scenarios like refactoring, importing, and debugging.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 31, 2025