terraform state identities command
The terraform state identities command lists the identities of resources within a
Terraform state.
Usage
Usage: terraform state identities [options] -json [address...]
This command lists the identities of resource instances in the Terraform state in JSON format. The address argument can be used to filter the instances by resource or module. If a resource has no identity, it is not listed. If no address is given, identities for all resource instances are listed.
The addresses must either be module addresses or resource addresses, such as:
aws_instance.exampleaws_instance.example[0]module.examplemodule.example[0]module.example.module.childmodule.example.aws_instance.example
If any of the resources or modules given as filter addresses do not exist in the state, Terraform returns an error.
The following flags are available:
-json- Outputs the identities in JSON format. This flag is required.-state=path- Path to the state file. By default, Terraform will consult the state of the currently-selected workspace.-id=id- Filters the results to only instances whoseidattribute equals the given string.
Example: All resource identities
This example lists identities of all resources in the state:
$ terraform state identities -json
{
"test_instance.foo": {"id": "my-foo-id"},
"test_instance.bar": {"id": "my-bar-id"},
"module.count[0].test_instance.count": {
"project": "my-project-count-0",
"role": "roles/viewer-count-0"
}
}
Example: Filter by resource address
This example only lists identities for the given resource address:
$ terraform state identities -json test_instance.foo
{
"test_instance.foo[0]": {"id": "my-foo-id-0"},
"test_instance.foo[1]": {"id": "my-foo-id-1"}
}
Example: Filter by module
This example lists identities of resources in the given module and any submodules:
$ terraform state identities -json module.nest
{
"module.nest.test_instance.nest": {
"project": "my-project-nest",
"role": "roles/viewer-nest"
},
"module.nest.module.subnest.test_instance.subnest": {
"project": "my-project-subnest",
"role": "roles/viewer-subnest"
}
}
Example: Filter by ID
This example only lists the identities of resources whose ID attribute matches the specified string.
$ terraform state identities -id=sg-1234abcd -json
{"test_instance.foo[0]": {"new-id": "my-foo-id-0"}}