hashicorp/terraform · error

input must not contain null list

Error message

input must not contain null list

What it means

Thrown by TransposeFunc.Impl (the HCL transpose() builtin) when iterating the input map of lists and finding that a value (which should be a list of strings) is null. transpose() cannot place a null list, so it errors.

Source

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

			Name: "values",
			Type: cty.Map(cty.List(cty.String)),
		},
	},
	Type:         function.StaticReturnType(cty.Map(cty.List(cty.String))),
	RefineResult: refineNotNull,
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		inputMap := args[0]
		if !inputMap.IsWhollyKnown() {
			return cty.UnknownVal(retType), nil
		}

		outputMap := make(map[string]cty.Value)
		tmpMap := make(map[string][]string)

		for it := inputMap.ElementIterator(); it.Next(); {
			inKey, inVal := it.Element()
			if inVal.IsNull() {
				return cty.MapValEmpty(cty.List(cty.String)), errors.New("input must not contain null list")
			}
			for iter := inVal.ElementIterator(); iter.Next(); {
				_, val := iter.Element()
				if val.IsNull() {
					return cty.MapValEmpty(cty.List(cty.String)), errors.New("input list must not contain null string")
				}
				if !val.Type().Equals(cty.String) {
					return cty.MapValEmpty(cty.List(cty.String)), errors.New("input must be a map of lists of strings")
				}

				outKey := val.AsString()
				if _, ok := tmpMap[outKey]; !ok {
					tmpMap[outKey] = make([]string, 0)
				}
				outVal := tmpMap[outKey]
				outVal = append(outVal, inKey.AsString())
				sort.Strings(outVal)
				tmpMap[outKey] = outVal

View on GitHub (pinned to c9def3e214)

Solutions

  1. Coalesce null entries to an empty list: { for k,v in var.m : k => coalesce(v, []) }.
  2. Drop null-valued keys with an if guard before transpose().
  3. Ensure every map value is a non-null list at the source.

Example fix

# before
locals { t = transpose(var.deps) }   # some values null
# after
locals {
  clean = { for k, v in var.deps : k => v if v != null }
  t     = transpose(local.clean)
}
Defensive patterns

Strategy: validation

Validate before calling

# strip null-valued entries before transpose
locals {
  clean = { for k, v in var.deps : k => v if v != null }
  t     = transpose(local.clean)
}

Type guard

locals {
  clean = { for k, v in var.deps : k => coalesce(v, []) }
  t     = transpose(local.clean)
}

Try / catch

locals { t = try(transpose({ for k, v in var.deps : k => v if v != null }), {}) }

Prevention

When it happens

Trigger: transpose({a = null, b = ["x"]}) where one map entry's list is null; a map where an optional key resolved to null.

Common situations: An optional variable defaulting to null for one key; a for-expression that emits null for some entries; data source returning null for a list attribute.

Related errors


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