Lesson 2 of 3 Importing, Inspecting and Troubleshooting

Inspecting State with the CLI

Terraform provides several commands for looking at what it manages. This lesson covers listing and showing state, displaying plans and outputs, and the commands that change state directly.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the commands that inspect state, plans and outputs.
  • Describe the state commands that change state directly.

Listing and showing resources

terraform state list prints the address of every resource instance in state, one per line, including data sources and resources inside modules. An address prefix filters the list, and -id finds the address that manages a given object.

$ terraform state list
data.aws_ami.ubuntu
aws_instance.web[0]
aws_instance.web[1]
module.network.aws_subnet.public["a"]
module.network.aws_vpc.this

$ terraform state list module.network
$ terraform state list -id=i-0abc1234def567890
$ terraform state show 'aws_instance.web[0]'

terraform state show ADDRESS prints every attribute of one instance in an HCL-like format, with sensitive values redacted. Both commands read the latest state snapshot; they do not refresh from the real infrastructure, so the values are as of the last plan or apply that wrote state.

The following diagram shows the two state commands and their output.

Finding which resource manages an EC2 instance. state list -id searches state by object ID and prints the address; state show prints the recorded attributes, including tags. state show reads the latest snapshot without refreshing, so a refresh-only plan confirms the tags still match reality.
The instance ID and address are illustrative.

terraform show and terraform output

terraform show displays the whole current state in human-readable form, or, given a plan file, the saved plan. With -json, it produces a machine-readable document that policy tools, cost tools and scripts consume. JSON output includes sensitive values in plain text.

terraform output displays root module outputs from state. The following table compares the inspection commands.

CommandShows
terraform state listAddresses of all resource instances
terraform state show ADDRESSAll attributes of one instance
terraform showThe whole state, or a saved plan with a file argument
terraform show -jsonState or plan as JSON, including sensitive values
terraform output [NAME]Root outputs; -raw and -json for scripts
terraform providersThe providers each module requires
terraform graphThe dependency graph in DOT format
Match the pairs

Inspection commands

Exam skills 7.2.1 · 7.2.2

Match each question with the command that answers it.

Log in to try this activity Interactive activities are free with an account.

Commands that change state

The remaining terraform state subcommands modify or transfer state directly. They lock state, write a backup and should be used carefully.

  • terraform state mv SOURCE DESTINATION changes an object's address, for example after a rename; a moved block is the reviewable alternative.
  • terraform state rm ADDRESS removes an object from state without destroying it; a removed block with destroy = false is the reviewable alternative.
  • terraform state replace-provider OLD NEW changes the provider source recorded for resources, for example when a provider moves to a new namespace.
  • terraform state pull prints the raw state from the backend, and terraform state push FILE overwrites remote state with a local file, which is dangerous and protected by lineage and serial checks.
Match the pairs

State-changing commands

Exam skills 7.2.3

Match each state subcommand with what it does.

Log in to try this activity Interactive activities are free with an account.
Worked example

Finding a resource

An auditor asks which Terraform resource manages EC2 instance i-0abc1234def567890 and what its tags are. The configuration has dozens of modules.

The decisive need is to search state by object ID.

terraform state list -id=i-0abc1234def567890 prints the address, for example module.app.aws_instance.web[1], and terraform state show 'module.app.aws_instance.web[1]' prints its attributes, including tags. Because state show does not refresh, a terraform plan -refresh-only first confirms that the recorded tags still match reality.

Check yourself

Does terraform state show refresh the resource before displaying it?

Show answer

No. It shows the attributes recorded in the latest state snapshot.

Exam Tips

Remember for the exam

  • state list for addresses (-id to search); state show for one instance.
  • state list and state show do not refresh.
  • show -json and state pull reveal sensitive values.
  • state mv/rm change state immediately; prefer moved/removed blocks.