You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
terraform/website/docs/cli/commands/state/list.mdx

95 lines
2.9 KiB

---
page_title: terraform state list command reference
description: >-
The terraform state list command lists resources within a Terraform
state.
---
⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
> [!IMPORTANT]
> **Documentation Update:** Product documentation previously located in `/website` has moved to the [`hashicorp/web-unified-docs`](https://github.com/hashicorp/web-unified-docs) repository, where all product documentation is now centralized. Please make contributions directly to `web-unified-docs`, since changes to `/website` in this repository will not appear on developer.hashicorp.com.
⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
# `terraform state list` command
The `terraform state list` command lists resources within a
[Terraform state](/terraform/language/state).
## Usage
Usage: `terraform state list [options] [address...]`
The command will list all resources in the state file matching the given
addresses (if any). If no addresses are given, all resources are listed.
The resources listed are sorted according to module depth order followed
alphabetically. This means that resources that are in your immediate
configuration are listed first, and resources that are more deeply nested
within modules are listed last.
For complex infrastructures, the state can contain thousands of resources.
To filter these, provide one or more patterns to the command. Patterns are
in [resource addressing format](/terraform/cli/state/resource-addressing).
The command-line flags are all optional. The following flags are available:
* `-state=path` - Path to the state file. Defaults to "terraform.tfstate".
Ignored when [remote state](/terraform/language/state/remote) is used.
* `-id=id` - ID of resources to show. Ignored when unset.
## Example: All Resources
This example will list all resources, including modules:
```
$ terraform state list
aws_instance.foo
aws_instance.bar[0]
aws_instance.bar[1]
module.elb.aws_elb.main
```
## Example: Filtering by Resource
This example will only list resources for the given name:
```
$ terraform state list aws_instance.bar
aws_instance.bar[0]
aws_instance.bar[1]
```
## Example: Filtering with an index
This example will show you how to filter when your filter includes an index:
```
$ terraform state list 'foo[0].bar'
foo[0].bar.lorem
foo[0].bar.ipsum
```
Use single quotes to prevent your command line from interpreting the square brackets as shell syntax.
## Example: Filtering by Module
This example will list resources in the given module and any submodules:
```
$ terraform state list module.elb
module.elb.aws_elb.main
module.elb.module.secgroups.aws_security_group.sg
```
## Example: Filtering by ID
This example will only list the resource whose ID is specified on the
command line. This is useful to find where in your configuration a
specific resource is located.
```
$ terraform state list -id=sg-1234abcd
module.elb.aws_security_group.sg
```