Bird
Raised Fist0
Terraformcloud~10 mins

Terraform show for state inspection - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Terraform show for state inspection
Terraform state file exists
↓
Run 'terraform show'
↓
Terraform reads state file
↓
Parse state data
↓
Display human-readable resource info
↓
User inspects resource details
↓
END
Terraform show reads the saved state file and displays resource details in a readable format for inspection.
Execution Sample
Terraform
terraform show terraform.tfstate
This command reads the Terraform state file and outputs the current infrastructure state details.
Process Table
StepActionInput/ConditionOutput/Result
1Check for state fileterraform.tfstate existsState file found
2Read state fileterraform.tfstate contentRaw state data loaded
3Parse state dataRaw JSON stateStructured resource info
4Format outputStructured resource infoHuman-readable resource details
5Display outputFormatted detailsUser sees resource attributes
6EndAll resources shownInspection complete
💡 All resources in the state file have been displayed for inspection.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
state_fileNot loadedLoaded raw JSONParsed JSON objectFormatted stringDisplayed output
resource_infoEmptyEmptyPopulated with resourcesFormatted for displayVisible to user
Key Moments - 3 Insights
Why does 'terraform show' need the state file?
'terraform show' reads the state file to know what resources exist and their details, as shown in execution_table step 1 and 2.
Is 'terraform show' showing live cloud resources?
No, it shows the last known state saved locally in the state file, not live cloud data, as seen in step 3 parsing the saved JSON.
What if the state file is missing or corrupted?
Terraform show will fail to read or parse the state file, so no output is shown. This is implied by step 1 checking for the file.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 3?
AStructured resource info
BFormatted human-readable details
CRaw JSON state data
DUser sees resource attributes
💡 Hint
Check the 'Output/Result' column for step 3 in the execution_table.
At which step does Terraform display the resource details to the user?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look for the step where 'User sees resource attributes' in the execution_table.
If the state file is missing, which step would fail?
AStep 4
BStep 5
CStep 1
DStep 6
💡 Hint
Step 1 checks if the state file exists before proceeding.
Concept Snapshot
terraform show <statefile>
Reads the Terraform state file
Parses resource info
Displays human-readable details
Used to inspect current infrastructure state
Does not query live cloud resources
Full Transcript
Terraform show is a command that reads the saved state file of your infrastructure. It first checks if the state file exists, then loads its raw JSON data. Next, it parses this data into structured resource information. After formatting this info into a readable form, it displays the resource details to the user. This lets you inspect what Terraform knows about your infrastructure without querying live cloud services. If the state file is missing or corrupted, the command will fail early.

Practice

(1/5)
1. What is the main purpose of the terraform show command?
easy
A. To apply changes to your infrastructure
B. To display the current state of your infrastructure managed by Terraform
C. To initialize a new Terraform working directory
D. To destroy all resources managed by Terraform

Solution

  1. Step 1: Understand the role of terraform show

    This command is used to display the saved state of your infrastructure, showing what Terraform currently knows about your resources.
  2. Step 2: Differentiate from other commands

    Commands like terraform apply, terraform init, and terraform destroy perform actions, but terraform show only displays information.
  3. Final Answer:

    To display the current state of your infrastructure managed by Terraform -> Option B
  4. Quick Check:

    terraform show = display state [OK]
Hint: Remember: show means display info, not change it [OK]
Common Mistakes:
  • Confusing show with apply or destroy commands
  • Thinking show modifies infrastructure
  • Assuming show initializes Terraform
2. Which of the following is the correct syntax to display the Terraform state in JSON format?
easy
A. terraform show -json
B. terraform show --json-output
C. terraform show json
D. terraform show -format=json

Solution

  1. Step 1: Recall the correct flag for JSON output

    The official Terraform command to show state in JSON format uses the -json flag.
  2. Step 2: Verify other options

    Options like --json-output, json as a subcommand, or -format=json are not valid Terraform syntax.
  3. Final Answer:

    terraform show -json -> Option A
  4. Quick Check:

    Use -json flag for JSON output [OK]
Hint: Use '-json' flag exactly for JSON output [OK]
Common Mistakes:
  • Adding extra words like '--json-output'
  • Using subcommands instead of flags
  • Mixing up flag syntax
3. Given the following Terraform state snippet shown by terraform show:
resource "aws_instance" "web" {
  id = "i-1234567890abcdef0"
  ami = "ami-0abcdef1234567890"
  instance_type = "t2.micro"
}
What will terraform show -json output include for the instance type?
medium
A. "instance_type": "t2.large"
B. "instance_type": "ami-0abcdef1234567890"
C. "instance_type": null
D. "instance_type": "t2.micro"

Solution

  1. Step 1: Identify the instance_type in the state snippet

    The snippet shows instance_type = "t2.micro" for the resource.
  2. Step 2: Understand JSON output reflects state values

    The JSON output from terraform show -json will include the exact value stored, which is "t2.micro".
  3. Final Answer:

    "instance_type": "t2.micro" -> Option D
  4. Quick Check:

    State value matches JSON output [OK]
Hint: JSON output matches exact state values [OK]
Common Mistakes:
  • Confusing AMI ID with instance type
  • Assuming default or null values
  • Mixing resource attributes
4. You run terraform show -json but get an error: error: no state file found. What is the most likely cause?
medium
A. Terraform is not installed
B. The -json flag is not supported
C. Terraform state file does not exist in the current directory
D. The configuration files have syntax errors

Solution

  1. Step 1: Understand the error message

    The error indicates Terraform cannot find the state file to show.
  2. Step 2: Identify common reasons for missing state

    If the state file is missing from the current directory, terraform show cannot display state.
  3. Final Answer:

    Terraform state file does not exist in the current directory -> Option C
  4. Quick Check:

    Missing state file causes this error [OK]
Hint: Check if state file exists before show [OK]
Common Mistakes:
  • Assuming -json flag is unsupported
  • Thinking Terraform is not installed
  • Blaming config syntax errors for state show
5. You want to programmatically extract the IDs of all AWS instances from your Terraform state. Which command and option combination is best suited for this task?
hard
A. terraform show -json | jq '.values.root_module.resources[] | select(.type=="aws_instance") | .values.id'
B. terraform show | grep aws_instance | awk '{print $2}'
C. terraform state list aws_instance
D. terraform output aws_instance_ids

Solution

  1. Step 1: Understand the need for programmatic extraction

    You want to get instance IDs from the state in a machine-readable way.
  2. Step 2: Evaluate command options

    terraform show -json outputs JSON, which can be filtered with jq to select AWS instances and extract their IDs. Other options either do not output JSON or do not extract IDs directly.
  3. Final Answer:

    terraform show -json | jq '.values.root_module.resources[] | select(.type=="aws_instance") | .values.id' -> Option A
  4. Quick Check:

    Use JSON output with jq for precise extraction [OK]
Hint: Use -json with jq to filter state data easily [OK]
Common Mistakes:
  • Using grep/awk on human-readable output (fragile)
  • Using terraform state list which lists names, not IDs
  • Assuming terraform output has instance IDs without explicit output