Skip to content

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?

  1. Aterraform providers
  2. Bterraform graph
  3. Cterraform state list
  4. 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.

Question 2 · choose 1

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?

  1. Aterraform state list 'module.web.aws_instance.app[0]'
  2. Bterraform show 'module.web.aws_instance.app[0]'
  3. Cterraform output 'module.web.aws_instance.app[0]'
  4. Dterraform state show 'module.web.aws_instance.app[0]'
Show the answer and why
  • Aterraform state list 'module.web.aws_instance.app[0]'

    Incorrect

    state list prints matching addresses, not attributes.

  • Bterraform show 'module.web.aws_instance.app[0]'

    Incorrect

    show takes a state or plan file path, not a resource address.

  • Cterraform output 'module.web.aws_instance.app[0]'

    Incorrect

    output reads output values only.

  • Dterraform state show 'module.web.aws_instance.app[0]'

    Correct

    state show displays the attributes of a single resource instance in state.

list finds addresses; show (under state) prints one resource.

Question 3 · choose 1

Which command prints the whole current state in a human-readable form?

  1. Aterraform show
  2. Bterraform output
  3. Cterraform graph
  4. Dterraform console
Show the answer and why
  • Aterraform show

    Correct

    terraform show provides human-readable output of the current state, or of a plan file.

  • Bterraform output

    Incorrect

    output prints the root module's output values, not the resources recorded in state.

  • Cterraform graph

    Incorrect

    graph prints the dependency graph, not state.

  • Dterraform console

    Incorrect

    console evaluates the expressions you type against the state; it does not print the state as a whole.

terraform show prints the current state, or a saved plan, for people to read.

Question 4 · choose 1

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?

  1. A-lock=false
  2. B-dry-run
  3. C-ignore-remote-version
  4. 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.

Question 5 · choose 1

A script parses the text output of terraform state show to read an IP address, and it breaks after an upgrade. What does the documentation say?

  1. Astate show output never changes, so the script has a bug
  2. BIts output is for people; use terraform show -json
  3. CPass -json to state show to get the same text as JSON
  4. DUse state list instead, which includes attributes
Show the answer and why
  • Astate show output never changes, so the script has a bug

    Incorrect

    Human-oriented output can change.

  • BIts output is for people; use terraform show -json

    Correct

    state show output is intended for human consumption; extract data for software with JSON outputs.

  • CPass -json to state show to get the same text as JSON

    Incorrect

    state show has no -json option documented.

  • DUse state list instead, which includes attributes

    Incorrect

    state list prints addresses only.

Parse JSON, not text meant for people.

Question 6 · choose 1

Do terraform state subcommands work the same when state lives in a remote backend?

  1. ANo; they work only with local state files
  2. BOnly state list works with remote state; the other subcommands need local state
  3. CYes, but only after state pull copies the state locally
  4. DYes; reads and writes just take a network roundtrip
Show the answer and why
  • ANo; they work only with local state files

    Incorrect

    They work with remote state.

  • BOnly state list works with remote state; the other subcommands need local state

    Incorrect

    All subcommands work with remote state.

  • CYes, but only after state pull copies the state locally

    Incorrect

    No manual copy is needed.

  • DYes; reads and writes just take a network roundtrip

    Correct

    The state subcommands work with remote state just as with local state, though reads and writes take longer.

Backend location does not change how the state CLI is used.

Question 7 · choose 1

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?

  1. Aterraform state show, which keeps the state unlocked while it prints
  2. Bterraform output, which edits the state
  3. Cterraform console, which locks state while open
  4. 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.

Practise domain 7 →Practise all domains →