IaC State Destruction Prevention & Safe Replacement¶
Purpose & Scope¶
This skill provides automated checks, live exploration commands, and self-learning guardrails to guarantee that infrastructure modifications never inadvertently destroy production databases, storage volumes, networking backbones, or stateful resources without human authorization.
Real-Life Production Incident Case: The Silent Aurora RDS Cluster Replacement¶
Background & What Happened¶
During a routine sprint, an engineer updated an Amazon Aurora PostgreSQL Terraform module to enable a new parameter group.
Because the parameter group family differed (postgres14 to postgres15), Terraform marked the underlying database cluster as:
-/+ resource "aws_rds_cluster" "primary" {
~ parameter_group_name = "default.aurora-postgresql14" -> "custom.aurora-postgresql15" # forces replacement
Real-Time Live Exploration Protocol¶
When a plan indicates potential state replacement, the agent must execute this real-time forensic exploration sequence:
1. Extract JSON State Delta & Identify Destruction Vectors¶
tofu plan -out=tfplan.binary
tofu show -json tfplan.binary > tfplan.json
# Extract exact resource addresses marked for deletion or replacement
jq -r '.resource_changes[] | select(.change.actions[] | contains("delete")) | "\(.address) -> \(.change.actions | join(","))"' tfplan.json
2. Drill Into the Specific "Forces Replacement" Attribute¶
# Isolate the exact attribute triggering replacement
jq -r '.resource_changes[] | select(.change.actions[] | contains("delete")) | .change.after_unknown, .change.before, .change.after' tfplan.json
3. Cross-Check with Live Cloud API (Verify Reality vs State)¶
Confirm whether the target cloud provider actually requires destruction or supports in-place modification:
# Query AWS API directly to inspect live resource configuration
aws rds describe-db-clusters --db-cluster-identifier <cluster-id> --query 'DBClusters[*].[Status,EngineVersion,DBClusterParameterGroup]'
Production Remediation Protocol¶
- State Preservation Locks: Enforce non-negotiable protection in root HCL:
- In-Place Modification via
movedBlocks: If a refactor changed module hierarchy or resource names: - State Disassociation Recovery: If a resource must be unlinked from state without destroying the live cloud resource:
Continuous Self-Knowledge Enhancement Loop¶
Whenever an agent discovers an unexpected attribute causing forces replacement:
1. Record the Incident Delta: Document the cloud provider's attribute immutability rule.
2. Update the Module Guardrail: Add a pre-apply check or HCL validation warning to the module's variables.tf.
3. Reflect in Project Knowledge: Append the discovered cloud trap to the project's root AGENTS.md under ## Learned Cloud Traps so all agents prevent the issue across future sprints.