hashicorp/terraform · error

input list must not contain null string

Error message

input list must not contain null string

What it means

Thrown by TransposeFunc.Impl when an element inside one of the inner lists is null. transpose() expects every inner element to be a string; a null element cannot become an output key.

Source

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

	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
			}
		}

		for outKey, outVal := range tmpMap {
			values := make([]cty.Value, 0)

View on GitHub (pinned to c9def3e214)

Solutions

  1. Run each inner list through compact() to strip nulls before transpose().
  2. Filter nulls in a for expression: [for s in list : s if s != null].
  3. Fix the upstream producer so it never emits null strings.

Example fix

# before
locals { t = transpose(var.deps) }   # inner lists contain nulls
# after
locals {
  clean = { for k, v in var.deps : k => compact(v) }
  t     = transpose(local.clean)
}
Defensive patterns

Strategy: validation

Validate before calling

# remove null elements from inner lists with compact()
locals {
  clean = { for k, v in var.deps : k => compact(v) }
  t     = transpose(local.clean)
}

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: transpose({a = ["x", null], b = ["y"]}); a list of strings containing a null because an upstream expression produced one.

Common situations: compact() not applied to a list that may contain nulls; optional object attributes yielding null strings; mixed data with gaps.

Related errors


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