hashicorp/terraform · error

input must not contain null list

Error message

input must not contain null list

What it means

Thrown by the `transpose()` function (TransposeFunc) when a value in the input map is null. transpose() expects a map of lists of strings; a null list at any key cannot be transposed, so it aborts with this message. It returns an empty map-shaped value alongside the error.

Solutions

  1. Filter out null values before transpose: `{ for k, v in var.m : k => v if v != null }`.
  2. Provide `[]` as the default instead of null for empty cases: `coalesce(v, [])`.
  3. Declare the variable type with non-optional list elements and defaults.

Example fix

// before
locals { t = transpose(var.groups) }   # some values null

// after
locals { t = transpose({ for k, v in var.groups : k => coalesce(v, []) }) }
Defensive patterns

Strategy: validation

Validate before calling

# HCL: drop null lists before transpose
locals { t = transpose({ for k, v in var.m : k => v if v != null }) }

Type guard

# HCL: coalesce nulls to empty lists
locals { t = transpose({ for k, v in var.m : k => coalesce(v, []) }) }

Prevention

When it happens

Trigger: Calling `transpose({a = null})` or a map variable where some value list is null, e.g. `{ roles = null }`.

Common situations: A map variable with optional keys whose unset values resolve to null. Conditional construction that produces null lists for some keys.

Related errors


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

Appendix: 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 d32a084675)