HashiCorp Terraform Associate (004) Question 190
Single answer7b Use the CLI to inspect stateYour team stores Terraform state remotely in an S3 backend. After a recent apply, a developer reports that an EC2 instance was created but is not sure which Terraform resource address in state maps to that instance. You need to inspect the current state without modifying infrastructure and view the attributes for a specific tracked resource. Which CLI command should you use?
- A
terraform state show aws_instance.web
- B
terraform show aws_instance.web
- C
terraform state list aws_instance.web
- D
terraform refresh aws_instance.web
Show answer and explanation
Correct answer: A
Explanation
To inspect Terraform state from the CLI, the key commands are terraform state list to enumerate resource addresses and terraform state show <resource_address> to display detailed state attributes for one resource instance. In this scenario, the goal is to inspect a specific tracked object without changing infrastructure, so terraform state show aws_instance.web is the correct choice. terraform show is useful for displaying the full state or a saved plan, but not for targeted lookup by resource address. HashiCorp documentation for the Terraform CLI state subcommands distinguishes these inspection tasks clearly: state list for addresses and state show for detailed resource data.
- A. Correct.
Correct.
terraform state show <address>displays the attributes of a single resource instance currently tracked in state, using its full resource address. It is intended for state inspection and does not modify infrastructure. This is the right command when you already know or suspect the resource address and want to inspect the stored state data for that object. - B. Incorrect.
Incorrect.
terraform showdisplays a human-readable view of the entire state or a plan file, but it does not accept a resource address argument in this way to inspect one specific state object. A candidate might choose this becauseterraform showis also used for inspection, but it is not the correct command for targeted resource-level state inspection. - C. Incorrect.
Incorrect.
terraform state listis used to list resource addresses in the state, which can help you discover the correct address, but it does not display detailed attributes for a resource. Someone might pick this if they are thinking about identifying resources in state, but the scenario specifically requires viewing the attributes for a tracked resource. - D. Incorrect.
Incorrect.
terraform refreshupdates the state to match real infrastructure and is not the appropriate command for simply inspecting state. In addition, using it with a resource address in this form is not how state inspection is performed. This option reflects the common misconception that refresh is an inspection command rather than a state reconciliation operation.