terraform-state-operations
Skill15 starsChanged 9 months agoMITView on GitHub
Use when performing Terraform state surgery - state mv, import, rm operations. Requires extra safety measures.
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 8 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈8
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 35
Before proceeding, create a record:
- Distinctive: line 89
Use case: Remove from state without destroying actual resource (adopting externally-managed resources)
- Distinctive: line 121
Write the resource configuration in code
- Distinctive: line 122
Ensure configuration matches actual resource
- Distinctive: line 124
Run plan to verify no changes
- Very specific: line 160
Import them back: terraform import ...
- Distinctive: line 161
Delete them manually if no longer needed
- Distinctive: line 189
Hook blocks this command: If genuinely needed:
- Distinctive: line 197
Before any state operation:
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
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:
# Create timestamped backup
BACKUP_NAME="state-backup-$(date +%Y%m%d-%H%M%S).tfstate"
# For local state
cp terraform.tfstate "$BACKUP_NAME"
# For remote state (S3 example)
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
# List current resources
terraform state list
# Move/rename a resource
terraform state mv aws_instance.old_name aws_instance.new_name
# Move into a module
terraform state mv aws_instance.web module.web.aws_instance.this
# Move between modules
terraform state mv module.old.aws_instance.web module.new.aws_instance.webVerification after mv:
# Should show no changes
terraform planterraform state rm
Use case: Remove from state without destroying actual resource (adopting externally-managed resources)
# Remove single resource
terraform state rm aws_instance.legacy
# Remove module
terraform state rm module.legacyWARNING: The actual resource continues to exist but is no longer managed by Terraform.
Verification after rm:
# Resource should appear as "new" if still in code
# Remove from code if intentionally unmanaging
terraform planterraform import
Use case: Bring existing infrastructure under Terraform management
# Import existing resource
terraform import aws_instance.web i-1234567890abcdef0
# Import with module path
terraform import module.web.aws_instance.this i-1234567890abcdef0Before import:
- Write the resource configuration in code
- Ensure configuration matches actual resource
- Import
- Run plan to verify no changes
Verification after import:
# Should show no changes (or only acceptable differences)
terraform planterraform state pull/push
Use case: Backup, migrate, or restore state
# Pull current state
terraform state pull > state.json
# Push state (DANGEROUS - can overwrite!)
# terraform state push state.json # BLOCKED by hookRecovery Procedures
If State Becomes Corrupted
# Restore from backup
cp "$BACKUP_NAME" terraform.tfstate
# For remote state - may need to disable locking temporarily
# Consult your backend documentationIf Resources Are Orphaned
- Identify orphaned resources in AWS console
- Either:
- Import them back:
terraform import ... - Delete them manually if no longer needed
- Import them back:
If Wrong Resources Removed
# Restore backup
cp "$BACKUP_NAME" terraform.tfstate
# Re-initialize to sync with backend
terraform init -reconfigureApproval 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:
- Backup created and verified
- Operation documented
- Impact explained to user
- User explicitly approved
- Rollback plan established
After operation:
- terraform plan shows expected result
- No unintended changes detected
- Backup retained for rollback window
- Operation logged to memory
In this repository20 other files it gives its agents.
- auto-documentationSkill
- aws-profile-managementSkill
- brainstormingSkill
- dispatching-parallel-agentsSkill
- executing-plansSkill
- finishing-a-development-branchSkill
- historical-pattern-analysisSkill
- provider-upgrade-analysisSkill
- receiving-code-reviewSkill
- requesting-code-reviewSkill
- subagent-driven-developmentSkill
- systematic-debuggingSkill
- terraform-drift-detectionSkill
- terraform-plan-reviewSkill
- test-driven-developmentSkill
- using-devops-skillsSkill
- using-git-worktreesSkill
- verification-before-completionSkill
- writing-plansSkill
- writing-skillsSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

