The terraform state list and terraform state show commands are essential tools for inspecting and managing your Terraform state file, which tracks the current state of your infrastructure.
**terraform state list**
This command displays all resources currently tracked in your Terraform state. It pβ¦The terraform state list and terraform state show commands are essential tools for inspecting and managing your Terraform state file, which tracks the current state of your infrastructure.
**terraform state list**
This command displays all resources currently tracked in your Terraform state. It provides a hierarchical list of resource addresses, making it easy to see what Terraform is managing. The output shows resources in the format: resource_type.resource_name (e.g., aws_instance.web_server).
Common use cases include:
- Auditing managed resources
- Finding specific resource addresses for other state operations
- Verifying resources were created successfully
- Troubleshooting state-related issues
You can filter results using partial addresses: terraform state list aws_instance will show only AWS instances.
**terraform state show**
This command reveals detailed information about a specific resource in your state. You must provide the full resource address as an argument (e.g., terraform state show aws_instance.web_server).
The output includes:
- All attributes and their current values
- Resource dependencies
- Provider information
- Metadata like resource ID
This is invaluable for:
- Debugging configuration issues
- Verifying attribute values
- Understanding resource relationships
- Comparing actual vs expected state
**Best Practices**
Both commands are read-only operations that do not modify your infrastructure or state. They work with both local and remote state backends. When using remote state, ensure proper authentication is configured.
For the Terraform Associate exam, understand that these commands help maintain infrastructure by providing visibility into what Terraform manages. They are fundamental for day-to-day operations, allowing teams to inspect resources before making changes and verify that apply operations completed as expected.
Remember that state contains sensitive information, so access should be controlled appropriately when working in team environments.
Terraform State List and Show Commands
Why This Is Important
Understanding terraform state list and show commands is essential for managing and troubleshooting Terraform infrastructure. These commands provide visibility into your state file, helping you understand what resources Terraform is tracking and their current configurations. For the Terraform Associate exam, you must demonstrate proficiency in inspecting and managing state.
What Are These Commands?
terraform state list - This command displays all resources currently tracked in your Terraform state file. It outputs resource addresses in a simple list format, making it easy to see what Terraform is managing.
terraform state show - This command displays detailed information about a single resource in your state file, including all attributes and their current values.
How They Work
terraform state list - Reads the state file and outputs all resource addresses - Syntax: terraform state list [options] [address] - Can filter results using a partial address - Example output: aws_instance.web aws_vpc.main aws_subnet.public
terraform state show - Requires a specific resource address as an argument - Syntax: terraform state show [options] ADDRESS - Displays all attributes stored in state for that resource - Example: terraform state show aws_instance.web shows instance ID, AMI, tags, and other attributes
Common Use Cases
1. Auditing Resources - Verify which resources Terraform manages 2. Troubleshooting - Inspect resource attributes when issues arise 3. Documentation - Retrieve current resource configurations 4. State Management - Identify resources before moving or removing them
Exam Tips: Answering Questions on terraform state list and show
1. Remember the difference: list shows all resources, show displays details of ONE specific resource
2. State show requires an address: You must provide a resource address to the show command
3. Read-only operations: Both commands only READ state; they do not modify it
4. Filtering with list: You can pass a partial address to filter results (e.g., terraform state list aws_instance)
5. Output format: state show outputs human-readable format by default, not JSON
6. Module resources: Resources in modules appear with module prefix (e.g., module.vpc.aws_subnet.main)
7. Watch for distractors: Exam questions may include made-up commands like terraform list or terraform show state - these are incorrect
8. State file location: These commands work with your configured backend, whether local or remote
9. Sensitive values: state show will display sensitive attribute values stored in state
10. Common exam scenarios: - Which command lists all managed resources? Answer: terraform state list - Which command shows attributes of a specific resource? Answer: terraform state show - Do these commands modify state? Answer: No, they are read-only