mediumIntermediateSymptom-first#count#index#empty#attribute

Index out of range — accessing empty resource attributes

This object does not have an attribute named "ids"

Likely source:configuration

Focused incident mode — strips away everything but this runbook.

Symptoms

  • This object does not have an attribute named "ids"
  • terraform plan fails with index out of range on empty resource
  • Reference to count or for_each on resource with 0 instances

Evidence to grab

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

  • Index error output

    terraform plan 2>&1 | grep -A5 'attribute named'
  • Resource instance count

    terraform state list | grep <RESOURCE> | wc -l

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

  • count = var.enabled ? 1 : 0 when var.enabled = false
  • for_each = toset([]) produces zero instances

Confirm before fixing

terraform state list | grep <RESOURCE>
READ

Check if any instances exist

Fix

terraform console -e 'length(<RESOURCE>) > 0 ? <RESOURCE>[0].id : null'
READ

Use a conditional to safely reference the attribute

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 -A5 'attribute named'
READ

Read the exact attribute error

2
terraform state list | grep <RESOURCE> | wc -l
READ

Count instances of the resource

3
terraform console -e 'length(<RESOURCE>)'
READ

Check if the resource has any instances

4
terraform validate
READ

Confirm the fix compiles

Prevention

  • Always check length() before indexing into resources with count or for_each.
  • Use try() or conditional expressions for optional references.
  • Test plans with the full set of variables (including disabled features).
  • Use terraform validate in CI to catch type errors.

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