Skip to main content
RunBook Academy

← All break/fix scenarios in Terraform

advancedterraform-plan~25 min

count Index Shift: Resources Destroyed and Recreated

Reported symptoms

  • The plan destroys and recreates many resources
  • The count.index shifted
  • The resource identity changed

Evidence

  • · The plan shows the change
  • · The resource addresses changed
  • · The count changed
Diagnosis and resolutionclick to reveal

Root cause

count is index-based. Removing an element from the list shifts the indices, causing the resources to be destroyed and recreated. This is the count antipattern.

Remediation

1. Do not apply. 2. Switch to for_each. 3. Re-plan. 4. Verify the plan is empty.

Verification

The resources are recreated with stable identities, the plan is empty, and the incident is documented.

Prevention

1. Use for_each with stable keys instead of count with index-based identity. 2. Test the change in development.

count Index Shift: Resources Destroyed and Recreated

count is index-based. Removing an element from the list shifts the indices, causing the resources to be destroyed and recreated. This is the count antipattern.