HashiCorp Terraform Associate (004) Question 193
Single answer7b Use the CLI to inspect stateYour team stores Terraform state remotely in an S3 backend. After a recent apply, an engineer reports that the EC2 instance in the production workspace has the wrong instance type. You want to inspect the current Terraform state from the CLI to confirm the exact attributes Terraform has recorded for that specific resource, without modifying infrastructure or editing the state. Which command should you run?
- A
terraform state list
- B
terraform show
- C
terraform state show aws_instance.web
- D
terraform output aws_instance.web
Show answer and explanation
Correct answer: C
Explanation
To inspect Terraform state from the CLI for a specific resource, the most precise command is terraform state show <resource_address>. This is commonly used when troubleshooting drift, validating what Terraform believes exists, or comparing recorded attributes against real infrastructure. terraform state list helps you discover resource addresses, while terraform show provides a broader view of the state or plan, but terraform state show is the best fit for focused inspection of one resource. According to Terraform CLI documentation, the terraform state subcommands are intended for advanced state inspection and management, and state show specifically prints the attributes of a single resource instance from the state.
- A. Incorrect.
Incorrect.
terraform state listonly lists the addresses of resources tracked in state, such asaws_instance.web, but it does not display the resource's stored attribute values likeinstance_type. A candidate might choose this because it is part of theterraform statesubcommands used for inspection, but it is insufficient when you need detailed resource data. - B. Incorrect.
Incorrect.
terraform showdisplays a human-readable representation of the current state or a plan file, and it can be useful for broad inspection. However, in this scenario you need to confirm the exact recorded attributes for one specific resource address directly from state.terraform state show aws_instance.webis the more targeted and appropriate command for that task. - C. Correct.
Correct.
terraform state show aws_instance.webdisplays the attributes Terraform currently has recorded in state for the resource at that address. This is the right CLI command when you need to inspect one resource's state data without changing infrastructure. It works with remote backends as well; Terraform reads the current state and shows the stored attributes for that resource. - D. Incorrect.
Incorrect.
terraform outputis used to display root module output values, not arbitrary resource attributes from state. Unless the configuration explicitly defines an output that exposes the instance type, this command cannot inspectaws_instance.webdirectly. This distractor reflects a common misunderstanding between outputs and state inspection.