highAdvancedSymptom-first#moved#import#state#refactor

Moved block missing — planned destroy for resource that still exists

terraform plan shows - destroy for a resource that is still live

Likely source:configurationdata-loss

Focused incident mode — strips away everything but this runbook.

Symptoms

  • terraform plan shows - destroy for a resource that is still live
  • Resource renamed in code but old state entry still exists
  • Plan shows create + destroy instead of in-place update

Evidence to grab

Collect these artifacts before you change anything — they prove (or rule out) each cause.

  • Plan showing destroy + create

    terraform plan 2>&1 | grep -E '(\+|\-)' | head -20
  • State list of resources

    terraform state list | grep <OLD-NAME>

Diagnostic flow

Run the steps in order, marking each claim confirmed or refuted. The evidence you gather feeds the ranked causes below.

Guided decision tree is ready.

Steps with commands, expected output, and pass/fail branches.

Ranked root causes

Evidence checklist

  • Code changed resource name from aws_instance.old to aws_instance.new
  • terraform state list still shows aws_instance.old
  • Plan shows destroy old + create new

Confirm before fixing

terraform state list | grep '<OLD-NAME>'
READ

Confirm old state entry exists

Fix

terraform state mv <OLD-ADDR> <NEW-ADDR>
CHANGE

Migrate state to the new resource address

Expected: Successfully moved 1 object(s)

Resolution runbook

Apply from top to bottom after the cause is confirmed. Review every command before running it — risk tags are shown on each.

1
terraform plan 2>&1 | grep 'will be destroyed' | head -10
READ

Identify resources that will be destroyed

2
terraform state list | grep <OLD-NAME>
READ

Confirm the old state entry

3
terraform state mv <OLD-ADDR> <NEW-ADDR>
CHANGE

Migrate state to match the new code

4
terraform plan
READ

Verify no unexpected destroys

Prevention

  • Always use moved blocks when renaming resources in Terraform >= 1.1.
  • Use terraform state mv for resources renamed before moved block support.
  • Run terraform plan before apply to catch unintended destroy/create cycles.
  • Add moved blocks to the same PR that renames resources.

Commands marked READ CHANGE DESTRUCTIVE — never run DESTRUCTIVE commands against production data you cannot restore. Copy → verify → execute.