hashicorp/terraform · error

item not found

Error message

item not found

What it means

Thrown by IndexFunc.Impl after iterating the entire list without an Equal match. The requested value simply is not present, so index() cannot return a position.

Source

Thrown at internal/lang/funcs/collection.go:217

		if args[0].LengthInt() == 0 { // Easy path
			return cty.NilVal, errors.New("cannot search an empty list")
		}

		for it := args[0].ElementIterator(); it.Next(); {
			i, v := it.Element()
			eq, err := stdlib.Equal(v, args[1])
			if err != nil {
				return cty.NilVal, err
			}
			if !eq.IsKnown() {
				return cty.UnknownVal(cty.Number), nil
			}
			if eq.True() {
				return i, nil
			}
		}
		return cty.NilVal, errors.New("item not found")

	},
})

// LookupFunc constructs a function that performs dynamic lookups of map types.
var LookupFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name:         "inputMap",
			Type:         cty.DynamicPseudoType,
			AllowMarked:  true,
			AllowUnknown: true,
		},
		{
			Name:         "key",
			Type:         cty.String,
			AllowMarked:  true,
			AllowUnknown: true,

View on GitHub (pinned to c9def3e214)

Solutions

  1. Verify the exact value and casing exist in the list.
  2. Use contains(list, value) to pre-check before index().
  3. Wrap in try() with a sentinel (e.g., -1) when absence is acceptable.

Example fix

# before
locals { i = index(var.envs, var.target_env) }   # target_env not present -> error
# after
locals {
  i = contains(var.envs, var.target_env) ? index(var.envs, var.target_env) : -1
}
Defensive patterns

Strategy: validation

Validate before calling

# pre-check presence to avoid the not-found error
locals {
  i = contains(var.list, var.target) ? index(var.list, var.target) : -1
}

Type guard

locals {
  present = contains(var.list, var.target)
  i       = local.present ? index(var.list, var.target) : -1
}

Try / catch

locals { i = try(index(var.list, var.target), -1) }

Prevention

When it happens

Trigger: index(var.list, "missing") where 'missing' never occurs; case mismatch (index(list, "Prod") when entries are 'prod'); searching for a number that exists only as a string.

Common situations: Typo or casing in the search value; value genuinely absent; type mismatch between the list element type and the search value.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/13fc22fb738ad8ea. Report an issue: GitHub.