Reading State
Reading state means asking Terraform what it recorded about the objects it manages. Three commands cover most of the need.
List every resource
The list uses the address of each resource.
terraform state listShow one resource
The output lists the stored attributes for that resource.
terraform state show local_file.helloPull the whole file
The output is the full state as JSON.
terraform state pullExample
# main.tf
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.0"
}
}
}
resource "local_file" "hello" {
content = "Hello."
filename = "${path.module}/hello.txt"
}
# commands
terraform init
terraform apply -auto-approve
terraform state list
terraform state show local_file.helloKey points
- terraform state list names every resource. It is the quickest overview.
- terraform state show prints one resource in full. It shows every stored attribute.
- terraform state pull prints the whole state file as JSON. It is useful for scripts.
- These commands never change anything. They only read.
- Read state before editing it by hand. Most days, reading is all that is needed.
The list shows one resource, and the show command prints its stored attributes. The file name and the content match the configuration. Nothing changed while reading.
Note
state pull output is raw JSON. Pipe it to a tool like jq to search it.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Which command prints the stored attributes of one resource?