Skip to content

002 · Domain 2: Terraform State and Backend Operations · 13% of the exam

Task 2.5: Inspect Terraform state

Reading state without changing it: state list filters and resource addresses, state show, terraform show and its JSON output, and count and for_each instance keys.

Study it

  • Reading state: state list, state show and terraform show -json

    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 audit script needs the address of every resource instance that belongs to module.network, including resources in modules nested inside it, as plain text with one address per line. Which command produces that output?

  1. Aterraform state list module.network
  2. Bterraform show module.network
  3. Cterraform state list -id=module.network
  4. Dterraform state list 'module.network.*'
Show the answer and why
  • Aterraform state list module.network

    Correct

    A module address as the pattern lists that module's resources and those of its nested modules, one address per line.

  • Bterraform show module.network

    Incorrect

    terraform show takes the path of a state or plan file, not a module address, and prints attributes rather than a list of addresses.

  • Cterraform state list -id=module.network

    Incorrect

    -id filters by the remote object ID, not by a module address.

  • Dterraform state list 'module.network.*'

    Incorrect

    Patterns use the resource address format, which has no wildcard segment; the module address alone is the filter.

Filtering terraform state list by a module path is the quick way to see everything under a module. Use terraform show -json when the output must be parsed as structured data.

Question 2 · choose 1

A pipeline converts each saved plan with terraform show -json and must block any plan that deletes a database, whether the deletion is a plain destroy, a normal replacement or a replacement under create_before_destroy. Which check on each resource_changes entry is correct?

  1. Achange.actions equals ["delete", "create"]
  2. Bchange.actions contains "delete"
  3. Caction_reason equals "replace_because_tainted"
  4. DThe address appears in resource_drift
Show the answer and why
  • Achange.actions equals ["delete", "create"]

    Incorrect

    That is only the default replacement order; a create_before_destroy replacement is ["create", "delete"], and a plain destroy is ["delete"].

  • Bchange.actions contains "delete"

    Correct

    The JSON format lists replacements as two actions so that callers can scan for "delete" and catch every case where the object will be deleted.

  • Caction_reason equals "replace_because_tainted"

    Incorrect

    action_reason is optional context and appears for specific reasons such as tainting, not for every deletion.

  • DThe address appears in resource_drift

    Incorrect

    resource_drift describes changes detected outside Terraform since the last run, not the actions the plan will take.

Valid action lists include ["delete", "create"], ["create", "delete"] and ["delete"]. Checking for "delete" anywhere in the list is the documented way to recognize all of them.

Question 3 · choose 1

In PowerShell, an engineer wants to show the "example" instance of packet_device.worker, which uses for_each. Following the terraform state show reference, which command is quoted correctly?

  1. Aterraform state show 'packet_device.worker[\"example\"]'
  2. Bterraform state show packet_device.worker[example]
  3. Cterraform state show packet_device.worker.example
  4. Dterraform state show packet_device.worker[0]
Show the answer and why
  • Aterraform state show 'packet_device.worker[\"example\"]'

    Correct

    HashiCorp's PowerShell example wraps the address in single quotes and escapes the inner double quotes with backslashes.

  • Bterraform state show packet_device.worker[example]

    Incorrect

    for_each keys are strings and must keep their double quotes.

  • Cterraform state show packet_device.worker.example

    Incorrect

    Instance keys use bracket notation, not attribute notation.

  • Dterraform state show packet_device.worker[0]

    Incorrect

    Numeric indexes belong to count; this resource uses for_each keys.

Quoting rules differ by shell: single quotes on Unix shells, escaped quotes with no outer quotes in cmd.exe, and single quotes with escaped inner quotes in PowerShell.

Question 4 · choose 1

A configuration uses provider "aws" and provider "aws" { alias = "dr" }. A script reads terraform show -json for the current state and must tell which resources the dr configuration manages. What does the values representation offer for that?

  1. Aprovider_name, which holds the full configuration address with its alias
  2. BNothing: provider_name holds only the provider's name, with no alias or module path
  3. CAn alias field next to each resource's type and name
  4. Dschema_version, which differs between provider configurations
Show the answer and why
  • Aprovider_name, which holds the full configuration address with its alias

    Incorrect

    provider_name is only the provider's name, with no module path and no alias.

  • BNothing: provider_name holds only the provider's name, with no alias or module path

    Correct

    provider_name exists so that a resource type can be interpreted unambiguously; it does not name the provider configuration.

  • CAn alias field next to each resource's type and name

    Incorrect

    Resources in the values representation have fields such as address, mode, type, name, index and provider_name, but no alias field.

  • Dschema_version, which differs between provider configurations

    Incorrect

    schema_version is the version of the resource type's schema, the same for every configuration of a provider.

The configuration representation in a plan's JSON gives each resource a provider_config_key into provider_config, where aliased configurations carry their alias.

Question 5 · choose 1

In what order does terraform state list print resources?

  1. AIn the order Terraform created them
  2. BIn dependency order, from the most depended-on resource
  3. CIn random order that changes between runs
  4. DBy module depth and then alphabetically, root module first
Show the answer and why
  • AIn the order Terraform created them

    Incorrect

    Creation order is not used for the listing.

  • BIn dependency order, from the most depended-on resource

    Incorrect

    The list is sorted, not ordered by the dependency graph.

  • CIn random order that changes between runs

    Incorrect

    The order is stable: module depth first, then alphabetical.

  • DBy module depth and then alphabetically, root module first

    Correct

    Resources in the root module are listed first and more deeply nested ones last, alphabetically within each level.

Filter long lists with address patterns, such as a module path, or with -id to find the address of a known object.

Question 6 · choose 1

A compliance script reads terraform show -json and must know which attributes of each resource are sensitive without printing them. Which field does it read?

  1. AThe sensitive flag on attributes in terraform providers schema -json
  2. BA sensitive key that terraform show adds to every output
  3. Csensitive_values, which lists only the attributes that are sensitive
  4. DA sensitive flag on each resource in the output of terraform output -json
Show the answer and why
  • AThe sensitive flag on attributes in terraform providers schema -json

    Incorrect

    The schema describes a provider's attributes in general, not which values are sensitive in a particular state or plan.

  • BA sensitive key that terraform show adds to every output

    Incorrect

    Outputs do mark sensitivity, but resource attributes use another field.

  • Csensitive_values, which lists only the attributes that are sensitive

    Correct

    The values representation includes sensitive_values, a structure with only the attributes that are sensitive.

  • DA sensitive flag on each resource in the output of terraform output -json

    Incorrect

    terraform output reports declared outputs, not the attributes of resources.

Use sensitive_values to decide which attributes a report may show.

Practice domain 2 →Practice all domains →