Terraform Version
Use Cases
-
Quick Resource Lookup: As a Terraform user, I often need to find specific resources in large state files without manually parsing JSON or using external tools like jq.
-
Debugging and Troubleshooting: When debugging infrastructure issues, I need to quickly locate resources by type, name, or attribute values to understand the current state.
-
State File Navigation: Large state files with hundreds of resources make it difficult to find specific resources without a dedicated search tool.
-
Attribute-Based Search: There's no built-in way to search for resources based on their attribute values (e.g., finding all resources with a specific IP address or tag).
-
Improved Developer Experience: A built-in search command would streamline workflows by eliminating the need to export state to JSON and use external tools for searching.
Attempted Solutions
-
Manual JSON Parsing: Used terraform show -json and manually searched through the output, which is time-consuming and error-prone for large state files.
-
External Tools: Used jq or other JSON processors to search state files, requiring additional tool installation and complex query syntax.
-
terraform state list: Used the existing terraform state list command, but it only lists resource addresses without search capabilities or attribute inspection.
-
terraform state show: Used terraform state show to inspect individual resources, but this requires knowing the exact resource address beforehand.
Proposal
Introduce a new Terraform command: terraform state find
Key Features:
- Search by Resource Type: Find all resources of a specific type (e.g.,
aws_instance, aws_s3_bucket)
- Search by Resource Name: Locate resources by their name or partial name match
- Search by Attribute Values: Find resources containing specific attribute values (e.g., IP addresses, tags, IDs)
- Flexible Matching: Support both substring and exact matching with case-insensitive search by default
- Multiple Output Formats: Display results in human-readable text or JSON format for scripting
terraform state find -h
Usage: terraform state find [options]
Search for resources in the Terraform state file by matching keywords
against resource addresses, types, and attribute values.
Options:
-state= Path to the state file (default: auto-detected)
-format=text|json Output format (default: text)
-exact Exact string match only (default: substring match)
-ignore-case=true|false Case-insensitive search (default: true)
Examples:
terraform state find aws_instance
terraform state find my-resource-name
terraform state find "10.0.0.1"
terraform state find -exact web-server
terraform state find -format=json security_group
terraform state find -state=/path/to/state postgres
Additional Features:
- Match Type Indication: Show where the match was found (type, name, address, or attribute)
- Attribute Field Highlighting: When matching on attributes, display which field contained the match
- Module Support: Search across resources in both root and child modules
- Comprehensive Results: Display resource address, type, name, module path, and match details
Example Scenarios:
- Search for a VPC by name:
terraform state find demo-vpc
Output:
Searching for 'demo-vpc' in state...
Found 1 resource(s):
=====================================
Address: ibm_is_vpc.main_vpc
Type: ibm_is_vpc
Name: main_vpc
Match: attribute
Matched Field: name
Attributes: 33 field(s)
=====================================
Total: 1 resource(s) matched
- Find all resources of a specific type:
terraform state find aws_instance
Output:
Searching for 'aws_instance' in state...
Found 3 resource(s):
=====================================
Address: aws_instance.web
Type: aws_instance
Name: web
Match: type
Address: aws_instance.app[0]
Type: aws_instance
Name: app
Match: type
Address: aws_instance.db
Type: aws_instance
Name: db
Match: type
=====================================
Total: 3 resource(s) matched
- Search for resources with a specific IP address:
terraform state find "10.0.1.5"
Output:
Searching for '10.0.1.5' in state...
Found 1 resource(s):
=====================================
Address: aws_instance.web
Type: aws_instance
Name: web
Match: attribute
Matched Field: private_ip
Attributes: 15 field(s)
=====================================
Total: 1 resource(s) matched
- Get JSON output for scripting:
terraform state find -format=json security_group
Output:
{
"results": [
{
"address": "aws_security_group.web",
"type": "aws_security_group",
"name": "web",
"match_type": "type"
}
],
"count": 1
}
References
- Related to state management improvements
- Complements existing
terraform state subcommands
- Addresses common workflow pain points in state file navigation
Terraform Version
Use Cases
Quick Resource Lookup: As a Terraform user, I often need to find specific resources in large state files without manually parsing JSON or using external tools like
jq.Debugging and Troubleshooting: When debugging infrastructure issues, I need to quickly locate resources by type, name, or attribute values to understand the current state.
State File Navigation: Large state files with hundreds of resources make it difficult to find specific resources without a dedicated search tool.
Attribute-Based Search: There's no built-in way to search for resources based on their attribute values (e.g., finding all resources with a specific IP address or tag).
Improved Developer Experience: A built-in search command would streamline workflows by eliminating the need to export state to JSON and use external tools for searching.
Attempted Solutions
Manual JSON Parsing: Used
terraform show -jsonand manually searched through the output, which is time-consuming and error-prone for large state files.External Tools: Used
jqor other JSON processors to search state files, requiring additional tool installation and complex query syntax.terraform state list: Used the existing
terraform state listcommand, but it only lists resource addresses without search capabilities or attribute inspection.terraform state show: Used
terraform state showto inspect individual resources, but this requires knowing the exact resource address beforehand.Proposal
Introduce a new Terraform command:
terraform state findKey Features:
aws_instance,aws_s3_bucket)terraform state find -hUsage: terraform state find [options]
Search for resources in the Terraform state file by matching keywords
against resource addresses, types, and attribute values.
Options:
-state= Path to the state file (default: auto-detected)
-format=text|json Output format (default: text)
-exact Exact string match only (default: substring match)
-ignore-case=true|false Case-insensitive search (default: true)
Examples:
terraform state find aws_instance
terraform state find my-resource-name
terraform state find "10.0.0.1"
terraform state find -exact web-server
terraform state find -format=json security_group
terraform state find -state=/path/to/state postgres
Additional Features:
Example Scenarios:
Output:
Output:
Output:
Output:
{ "results": [ { "address": "aws_security_group.web", "type": "aws_security_group", "name": "web", "match_type": "type" } ], "count": 1 }References
terraform statesubcommands