How to Use Terraform State List Command Effectively
Use the
terraform state list command to display all resources tracked in your Terraform state file. This helps you see what infrastructure Terraform manages and can filter resources by address or module.Syntax
The basic syntax of the terraform state list command is simple and includes optional filters.
terraform state list: Lists all resources in the current state.terraform state list [address]: Lists resources filtered by the given address or module path.
bash
terraform state list [options] [ADDRESS]
Example
This example shows how to list all resources in the Terraform state and how to filter by a specific resource address.
bash
terraform state list terraform state list aws_instance.example
Output
aws_instance.example
aws_s3_bucket.my_bucket
aws_instance.example
Common Pitfalls
Common mistakes when using terraform state list include:
- Running the command outside the Terraform working directory or without a valid state file, which results in errors.
- Using incorrect resource addresses or module paths, leading to no results.
- Confusing
terraform state listwithterraform show, which shows detailed resource info instead of just names.
bash
Wrong usage: terraform state list aws_instance.wrong_name Correct usage: terraform state list aws_instance.example
Quick Reference
| Command | Description |
|---|---|
| terraform state list | List all resources in the current state |
| terraform state list aws_instance.example | List specific resource by address |
| terraform state list module.network.aws_subnet.subnet1 | List resource inside a module |
| terraform state list -state=custom.tfstate | List resources from a specific state file |
Key Takeaways
Use
terraform state list to see all resources Terraform manages in the current state.You can filter the list by resource address or module path to find specific resources.
Run the command inside your Terraform working directory with a valid state file.
Incorrect addresses or running outside the state directory will cause errors or empty results.
This command helps verify what Terraform tracks before making changes.