004 · Domain 7: Maintain infrastructure with Terraform · 12% of the exam
Task 7.2: Use the CLI to inspect state
Reading state safely: terraform state list and show, terraform show, terraform output, state pull, and the state subcommands that change state (mv, rm, replace-provider, push) and their risks.
Study it
Inspecting and editing state with the CLI
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
An engineer needs the exact address of every resource Terraform manages in a configuration, including those inside modules, to use in a -replace option. Which command lists them?
Aterraform providers
Bterraform graph
Cterraform state list
Dterraform output
Show the answer and why
Aterraform providers
Incorrect
That shows provider requirements.
Bterraform graph
Incorrect
graph prints the dependency graph in DOT format, not a list of state addresses.
Cterraform state list
Correct
state list lists all resources in the state, including resources in modules, optionally filtered by address.
Dterraform output
Incorrect
output prints output values.
state list is the quickest way to get addresses for other commands.
An engineer wants to see all recorded attributes of the instance module.web.aws_instance.app[0] without reading the whole state. Which command does this?
Aterraform state list 'module.web.aws_instance.app[0]'
An engineer is about to run terraform state mv module.net module.network, which matches dozens of resource instances. Before anything changes, the engineer wants Terraform to report which instances the address matches, without moving them. Which option does this?
A-lock=false
B-dry-run
C-ignore-remote-version
D-compact-warnings
Show the answer and why
A-lock=false
Incorrect
That skips the state lock and still moves everything, which is risky if anyone else runs Terraform at the same time.
B-dry-run
Correct
-dry-run reports every resource instance that matches the given address and leaves the state unchanged.
C-ignore-remote-version
Incorrect
That option relates to the HCP Terraform integration and the remote backend; it does not turn the command into a preview.
D-compact-warnings
Incorrect
That is a plan and apply option for shorter warnings; state mv does not accept it.
State subcommands change state immediately, with no plan in between, so a dry run is the way to check what an address matches before committing to it.
An engineer wants to evaluate aws_instance.web.private_ip interactively, using the values currently in state. Which command fits, and what does it do to the state while open?
Aterraform state show, which keeps the state unlocked while it prints
Bterraform output, which edits the state
Cterraform console, which locks state while open
Dterraform validate, which reads state values
Show the answer and why
Aterraform state show, which keeps the state unlocked while it prints
Incorrect
state show prints a resource; it does not evaluate expressions.
Bterraform output, which edits the state
Incorrect
output reads output values only.
Cterraform console, which locks state while open
Correct
console evaluates expressions against values saved in state and holds a state lock while open.
Dterraform validate, which reads state values
Incorrect
validate does not read state.
console is for expressions, including references to things in state.