| name | terraform-state-operations |
| description | Use when performing Terraform state surgery - state mv, import, rm operations. Requires extra safety measures. |
Terraform State Operations
Overview
State operations modify Terraform's understanding of infrastructure without changing actual resources. These are dangerous because mistakes can orphan resources or cause Terraform to recreate existing infrastructure.
Announce at start: "I'm using the terraform-state-operations skill for safe state surgery."
CRITICAL: Pre-Operation Safety
1. Create State Backup
ALWAYS create a backup before ANY state operation:
BACKUP_NAME="state-backup-$(date +%Y%m%d-%H%M%S).tfstate"
cp terraform.tfstate "$BACKUP_NAME"
terraform state pull > "$BACKUP_NAME"
echo "Backup created: $BACKUP_NAME"
2. Document the Operation
Before proceeding, create a record:
## State Operation Record
**Date:** [timestamp]
**Environment:** [env name]
**Operator:** [user]
**Reason:** [why this operation is needed]
### Planned Operations
1. [operation 1]
2. [operation 2]
### Backup Location
- Local: [path]
- Remote: [if applicable]
### Rollback Plan
[How to restore if something goes wrong]
3. Get User Approval
Present the plan and require explicit approval before executing.
State Operations Guide
terraform state mv
Use case: Rename resources, reorganize modules, refactor code
terraform state list
terraform state mv aws_instance.old_name aws_instance.new_name
terraform state mv aws_instance.web module.web.aws_instance.this
terraform state mv module.old.aws_instance.web module.new.aws_instance.web
Verification after mv:
terraform plan
terraform state rm
Use case: Remove from state without destroying actual resource (adopting externally-managed resources)
terraform state rm aws_instance.legacy
terraform state rm module.legacy
WARNING: The actual resource continues to exist but is no longer managed by Terraform.
Verification after rm:
terraform plan
terraform import
Use case: Bring existing infrastructure under Terraform management
terraform import aws_instance.web i-1234567890abcdef0
terraform import module.web.aws_instance.this i-1234567890abcdef0
Before import:
- Write the resource configuration in code
- Ensure configuration matches actual resource
- Import
- Run plan to verify no changes
Verification after import:
terraform plan
terraform state pull/push
Use case: Backup, migrate, or restore state
terraform state pull > state.json
Recovery Procedures
If State Becomes Corrupted
cp "$BACKUP_NAME" terraform.tfstate
If Resources Are Orphaned
- Identify orphaned resources in AWS console
- Either:
- Import them back:
terraform import ...
- Delete them manually if no longer needed
If Wrong Resources Removed
cp "$BACKUP_NAME" terraform.tfstate
terraform init -reconfigure
Approval Workflow
For state mv (Low Risk)
- Show planned move
- Explain impact
- Request approval
- Execute with verification
For state rm (Medium Risk)
- Show what will be unmanaged
- Explain orphan implications
- Confirm resource won't be deleted
- Request explicit approval
- Execute with verification
For state push (High Risk)
Hook blocks this command. If genuinely needed:
- Explain why state push is necessary
- Show state diff
- Request explicit approval with acknowledgment of risks
- User must disable hook manually
Verification Checklist
Before any state operation:
After operation: