TExcelTAD
Home › Infrastructure as Code: Terraform & OpenTofu › Inspecting & Editing State

Inspecting & Editing State

Never edit the state file by hand. The state subcommands read and change it safely: they take the lock, bump the serial and keep a backup. Most days you only read; the editing commands are for refactors and repairs.

Command Does
tofu state list [ADDR] list resource addresses (optionally filtered)
tofu state show ADDR all attributes of one resource
tofu show [-json] the whole state
tofu state mv SRC DST rename an address without touching the real object
tofu state rm ADDR forget an object: it stays in the cloud, no longer managed
tofu state pull / push download / upload the raw state (backups, emergency repair)
tofu state replace-provider OLD NEW point resources at a different provider source

Reading

bash · try it yourself
mkdir s && cd s
cat > main.tf <<'HCL'
resource "local_file" "env" {
  for_each = toset(["dev", "prod"])
  filename = "${each.key}.env"
  content  = "ENV=${each.key}\n"
}

resource "terraform_data" "release" {
  input = "1.4.0"
}
HCL
tofu init >/dev/null && tofu apply -auto-approve >/dev/null
tofu state list
echo "--- filtered"
tofu state list 'local_file.env'
echo "--- one resource"
tofu state show 'local_file.env["prod"]' | grep -v -E 'content_(md5|sha|base64)'
local_file.env["dev"]
local_file.env["prod"]
terraform_data.release
--- filtered
local_file.env["dev"]
local_file.env["prod"]
--- one resource
# local_file.env["prod"]:
resource "local_file" "env" {
    content              = <<-EOT
        ENV=prod
    EOT
    directory_permission = "0777"
    file_permission      = "0777"
    filename             = "prod.env"
    id                   = "dc21a19e3c2094476bef9d90996ab6e9597a413e"
}

Quote addresses with [" "] in single quotes so the shell leaves them alone.

Renaming: state mv

You renamed terraform_data.release to terraform_data.app_release in the code. Without help, the plan would destroy the old one and create the new one. state mv moves the object in state to the new address, so the plan is empty:

bash · try it yourself
mkdir s && cd s
cat > main.tf <<'HCL'
resource "terraform_data" "release" {
  input = "1.4.0"
}
HCL
tofu init >/dev/null && tofu apply -auto-approve >/dev/null
sed -i 's/"release"/"app_release"/' main.tf
echo "--- plan after renaming in code only"
tofu plan | grep -E '^  #|^Plan:'
tofu state mv terraform_data.release terraform_data.app_release
echo "--- plan after state mv"
tofu plan | tail -1
--- plan after renaming in code only
  # terraform_data.app_release will be created
  # terraform_data.release will be destroyed
  # (because terraform_data.release is not in configuration)
Plan: 1 to add, 0 to change, 1 to destroy.
Move "terraform_data.release" to "terraform_data.app_release"
Successfully moved 1 object(s).
--- plan after state mv
No changes. Your infrastructure matches the configuration.

state mv works, but it is an out-of-band change: it happens on someone's machine, isn't reviewed, and has to be repeated for every environment's state. The declarative moved block does the same thing in code (Refactoring with moved & removed), and is what you should use today.

Forgetting: state rm

state rm removes an object from state without destroying it. Use it when a resource should be managed somewhere else (another configuration will import it) or should no longer be managed at all:

bash · try it yourself
mkdir s && cd s
cat > main.tf <<'HCL'
resource "local_file" "keep" {
  filename = "keep.txt"
  content  = "still here\n"
}
HCL
tofu init >/dev/null && tofu apply -auto-approve >/dev/null
tofu state rm local_file.keep
tofu state list | wc -l
cat keep.txt
echo "--- the code still has the block, so the plan wants to create it again"
tofu plan | grep -E '^  #|^Plan:'
Removed local_file.keep
Successfully removed 1 resource instance(s).
0
still here
--- the code still has the block, so the plan wants to create it again
  # local_file.keep will be created
Plan: 1 to add, 0 to change, 0 to destroy.

The file still exists, but the state no longer knows it, so the next plan wants to create it again: remove the block from the code too (or use a removed block, which does both declaratively).

Backups before surgery

bash
tofu state pull > before-surgery.tfstate      # works for every backend, including S3
# … state mv / rm / import …
tofu plan                                     # must show exactly what you expect

If a repair goes wrong, tofu state push before-surgery.tfstate restores it (refused if the serial or lineage doesn't fit, unless you add -force, which you should think about twice). Remote backends with versioning (S3 bucket versioning) give you a second safety net.

Test yourself

What happens to the real object after tofu state rm aws_s3_bucket.logs?

  1. It is deleted
  2. Nothing: it stays in AWS but is no longer managed
  3. It is moved to another state
  4. It is renamed

You renamed a resource block in code. What avoids a destroy-and-create?

  1. tofu apply -replace
  2. A moved block (or tofu state mv)
  3. tofu refresh
  4. depends_on

Why is a moved block preferred over tofu state mv?

  1. It is faster
  2. It is code: reviewed, versioned and applied the same way to every environment
  3. state mv is deprecated
  4. moved also renames the cloud object