TF-004 Read, generate and modify configuration Practice Question
A developer needs to retrieve the current state of an AWS EC2 instance that was created by Terraform but the configuration file is missing. Which command will output the attributes of the instance in a format suitable for generating a configuration?
⚠ Common exam trap
Candidates often confuse `terraform state show` with `terraform output` or `terraform plan`, assuming any command that displays state information can generate a configuration, but only `terraform state show -json` provides the precise attribute mapping needed for configuration reconstruction.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
terraform state show -json aws_instance.example
`terraform state show -json aws_instance.example` retrieves the current state of the specified resource from the Terraform state file and outputs it in JSON format, which can be directly used to reconstruct a configuration block. This command is specifically designed for inspecting a single resource's attributes when the original configuration is unavailable.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
terraform output aws_instance
Why it's wrong here
The `terraform output` command is designed to display values explicitly defined within `output` blocks in your Terraform configuration. It does not provide a mechanism to query or inspect the attributes of arbitrary resources directly from the state file. To view attributes of an `aws_instance` using `terraform output`, those specific attributes would first need to be exposed via a named output definition.
- ✗
terraform state pull | grep aws_instance
Why it's wrong here
The `terraform state pull` command retrieves the entire remote state file and prints its raw content to standard output. While piping this output to `grep` might locate the resource name, this method is highly inefficient, unreliable, and error-prone for extracting specific resource attributes. The raw state file format is not guaranteed to be stable or easily parsable for programmatic data extraction, making it unsuitable for precise attribute retrieval.
- ✓
terraform state show -json aws_instance.example
Why this is correct
The `terraform state show` command is specifically designed to display the current attributes of a particular resource instance as recorded in the Terraform state file. By specifying `aws_instance.example` and including the `-json` flag, the command outputs all attributes of that exact resource in a structured, machine-readable JSON format. This precise and standardized output is ideal for scripting, automation, or direct consumption by other tools needing current state attributes.
- ✗
terraform plan
Why it's wrong here
The `terraform plan` command's primary function is to compute and display the *proposed changes* that Terraform will make to your infrastructure based on your configuration and the current state. It compares the desired state with the actual infrastructure and the existing state file, but it does not output the current attributes of existing resources in a directly consumable format. Its focus is on showing what *will* happen, not on detailing the current state attributes for inspection.
Go deeper
Related to this question
About these practice questions
This TF-004 question is part of Courseiva's 428-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.