HashiCorp Terraform Associate (004) Question 191
Single answer7b Use the CLI to inspect stateA platform engineer is troubleshooting a Terraform-managed AWS environment after a teammate manually changed tags on an existing EC2 instance in the AWS console. The engineer wants to inspect the current Terraform state to confirm the exact resource address and view the attributes Terraform has stored for that specific instance, without making any infrastructure changes. Which CLI command is the best choice?
- A
terraform state list
- B
terraform state show aws_instance.web
- C
terraform show -json
- D
terraform refresh
Show answer and explanation
Correct answer: B
Explanation
The best answer is terraform state show aws_instance.web because the requirement is to inspect the current Terraform state for a specific resource and view the stored attributes associated with that resource address. In practice, engineers often use terraform state list first to discover addresses, then terraform state show <address> to inspect one resource in detail. By contrast, terraform show is broader and often used to render human-readable or JSON output for the entire state or a plan, while terraform refresh is not a pure inspection command because it reconciles state with remote objects. This aligns with Terraform CLI state inspection workflows documented in HashiCorp Terraform CLI guidance for terraform state list, terraform state show, and terraform show.
- A. Incorrect.
Incorrect.
terraform state listlists resource addresses stored in state, which can help identify what Terraform is tracking, but it does not display the full stored attributes for a specific resource. In this scenario, the engineer needs both inspection of a specific resource and its stored details. - B. 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 most direct CLI command for inspecting a specific resource in state without modifying infrastructure. - C. Incorrect.
Incorrect.
terraform show -jsoncan output the full state or plan data in JSON form, which is useful for automation and tooling, but it is not the best targeted command for interactively inspecting one specific resource address during troubleshooting. It also requires additional parsing to isolate the desired resource. - D. Incorrect.
Incorrect.
terraform refreshupdates the state to match real infrastructure, which is a state-modifying operation rather than a read-only inspection command. The question specifically asks to inspect the current stored state without making infrastructure changes or updating state.