How to Use Terraform State Show Command
Use the
terraform state show <resource_address> command to display detailed information about a specific resource in your Terraform state file. This helps you inspect the current state and attributes of that resource.Syntax
The basic syntax of the terraform state show command is:
terraform state show <resource_address>: Shows detailed info about the resource at the given address.
resource_address is the identifier of the resource in your Terraform state, like aws_instance.example.
bash
terraform state show <resource_address>
Example
This example shows how to use terraform state show to view details of an AWS EC2 instance resource named aws_instance.example in your Terraform state.
bash
terraform state show aws_instance.example
Output
id = "i-0abcd1234efgh5678"
ami = "ami-0abcdef1234567890"
instance_type = "t2.micro"
availability_zone = "us-west-2a"
private_ip = "10.0.0.5"
public_ip = "54.123.45.67"
...
Common Pitfalls
1. Using wrong resource address: If you provide a resource address that does not exist in the state, Terraform will return an error.
2. Confusing resource names: Make sure to use the exact resource address as defined in your Terraform configuration and state.
3. State file not initialized or missing: The command requires a valid Terraform state file in the current directory.
bash
terraform state show aws_instance.wrong_name # Error: No resource found at aws_instance.wrong_name # Correct usage: terraform state show aws_instance.example
Quick Reference
terraform state show <resource_address>: Show resource details.- Use
terraform state listto find resource addresses. - Run command in directory with Terraform state file.
Key Takeaways
Use
terraform state show <resource_address> to inspect resource details in the state.Ensure the resource address matches exactly what is in your Terraform state.
Run the command in the directory containing your Terraform state file.
Use
terraform state list to find valid resource addresses.Incorrect resource addresses cause errors and no output.