Interview Prep/Terraform/Plan Errors and Unexpected Replacements

Plan says `aws_db_instance.main must be replaced` after changing `storage_encrypted = true`.

AdvancedTroubleshootingPlan Errors and Unexpected Replacements
Answer

Likely causes → Encryption at rest can't be enabled in place on RDS; the provider marks it forces replacement. Applying would create a new empty DB and delete the old one.

Diagnose → terraform plan | grep -B2 'forces replacement'

Fix → Revert the change. Migrate deliberately: snapshot → copy snapshot with KMS encryption → restore to a new instance (in code, snapshot_identifier) → switch the app → remove the old one.

Prevent → lifecycle { prevent_destroy = true } and deletion_protection = true on stateful resources, and a CI gate that blocks any delete on RDS/S3/EFS/DynamoDB.

All Terraform questions

More on Plan Errors and Unexpected Replacements