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?
Aterraform state list module.network
Bterraform show module.network
Cterraform state list -id=module.network
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.
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?
Achange.actions equals ["delete", "create"]
Bchange.actions contains "delete"
Caction_reason equals "replace_because_tainted"
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.
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?
Aterraform state show 'packet_device.worker[\"example\"]'
Bterraform state show packet_device.worker[example]
Cterraform state show packet_device.worker.example
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.
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?
Aprovider_name, which holds the full configuration address with its alias
BNothing: provider_name holds only the provider's name, with no alias or module path
CAn alias field next to each resource's type and name
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.
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?
AThe sensitive flag on attributes in terraform providers schema -json
BA sensitive key that terraform show adds to every output
Csensitive_values, which lists only the attributes that are sensitive
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.