States Commands
The stategraph states command group manages Terraform states in Stategraph.
Commands
| Command | Description |
|---|---|
stategraph states create |
Create a new state |
stategraph states list |
List states for a tenant |
stategraph states import |
Import a Terraform state file |
stategraph states summary |
Get state summary statistics |
stategraph states resources summary |
Get resource type counts |
stategraph states instances |
Instance operations |
stategraph states modules list |
List modules in a state |
stategraph states create
Create a new empty state.
stategraph states create --tenant <tenant-id> --name <state-name> [options]
Options
| Option | Required | Description |
|---|---|---|
--tenant |
Yes | Tenant ID (UUID) |
--name |
Yes | Name for the state |
--group |
No | Group ID to create state in |
--workspace |
No | Workspace name |
Example
stategraph states create --tenant 550e8400-e29b-41d4-a716-446655440000 --name "networking"
Output (JSON):
{
"id": "...",
"name": "networking",
"group_id": "...",
"workspace": "default",
"created_at": "2024-01-15T10:30:00Z"
}
stategraph states list
List all states for a tenant.
stategraph states list --tenant <tenant-id>
Options
| Option | Required | Description |
|---|---|---|
--tenant |
Yes | Tenant ID (UUID) |
Example
stategraph states list --tenant 550e8400-e29b-41d4-a716-446655440000
Output (JSON):
{
"results": [
{
"id": "...",
"name": "networking",
"group_id": "networking",
"workspace": "production",
"created_at": "2024-01-15T10:30:00Z"
}
]
}
stategraph states import
Import an existing Terraform state file.
stategraph states import --tenant <tenant-id> --name <state-name> [options] <state-file>
Options
| Option | Required | Description |
|---|---|---|
--tenant |
Yes | Tenant ID (UUID) |
--name |
Yes | Name for the state |
--group |
No | Group ID to create state in |
--workspace |
No | Workspace name |
--tag |
No | Tags (key=value format, repeatable) |
--tags |
No | Tags as JSON object |
Example
# Import with tags
stategraph states import \
--tenant 550e8400-e29b-41d4-a716-446655440000 \
--name "networking" \
--workspace "production" \
--tag environment=prod \
--tag team=platform \
terraform.tfstate
stategraph states summary
Get summary statistics for a state.
stategraph states summary --state <state-id>
Options
| Option | Required | Description |
|---|---|---|
--state |
Yes | State ID (UUID) |
Example
stategraph states summary --state 550e8400-e29b-41d4-a716-446655440000
Output (tab-separated):
Edges 234
Instances 150
Modules 8
Providers 3
Resources 45
stategraph states resources summary
Get instance counts by resource type.
stategraph states resources summary --state <state-id>
Options
| Option | Required | Description |
|---|---|---|
--state |
Yes | State ID (UUID) |
Example
stategraph states resources summary --state 550e8400-e29b-41d4-a716-446655440000
Output (tab-separated):
aws_instance 20
aws_security_group 15
aws_subnet 6
aws_iam_role 12
stategraph states instances
Instance-related operations.
stategraph states instances blast-radius
Get the blast radius for a specific instance.
stategraph states instances blast-radius --state <state-id> <instance-address>
Options
| Option | Required | Description |
|---|---|---|
--state |
Yes | State ID (UUID) |
Example
stategraph states instances blast-radius \
--state 550e8400-e29b-41d4-a716-446655440000 \
"aws_vpc.main"
Output (tab-separated):
aws_subnet.public aws_subnet.public[0] 0 1
aws_subnet.private aws_subnet.private[0] 0 1
aws_instance.web aws_instance.web[0] 0 2
Columns: resource_address, address, index, distance
stategraph states instances query
Query instances with a filter.
stategraph states instances query --state <state-id> [-i] <query>
Options
| Option | Required | Description |
|---|---|---|
--state |
Yes | State ID (UUID) |
-i |
No | Iterate through all pages |
Example
stategraph states instances query \
--state 550e8400-e29b-41d4-a716-446655440000 \
"type = 'aws_instance'"
stategraph states modules list
List modules in a state.
stategraph states modules list --state <state-id>
Options
| Option | Required | Description |
|---|---|---|
--state |
Yes | State ID (UUID) |
Example
stategraph states modules list --state 550e8400-e29b-41d4-a716-446655440000
Output (tab-separated):
module.vpc 10 25
module.eks 8 45
module.rds 4 12
Columns: name, resource_count, instance_count
Scripting Examples
Export all states to JSON
stategraph states list --tenant $TENANT_ID | jq -r '.results[] | .id' | while read state_id; do
echo "Exporting $state_id..."
stategraph states summary --state "$state_id"
done
Find states with specific resource types
stategraph states list --tenant $TENANT_ID | jq -r '.results[] | .id' | while read state_id; do
count=$(stategraph states resources summary --state "$state_id" 2>/dev/null | grep aws_rds_cluster | awk '{print $2}')
if [ -n "$count" ] && [ "$count" -gt 0 ]; then
echo "State $state_id has $count RDS clusters"
fi
done
Next Steps
- Transactions - Manage transactions
- MQL - Query infrastructure
- Backend - Terraform backend configuration