hashicorp/terraform · error
lookup failed to find key
Error message
lookup failed to find key %s
What it means
lookup() runtime error: the requested key was not present in the map/object and no default value was supplied. The function searches object attributes and map keys; if HasIndex/HasAttribute is false and defaultValueSet is false, it returns this error. The key is passed through redactIfSensitive so sensitive keys are not leaked.
Solutions
- Add a third argument as the default: lookup(var.map, "key", "fallback").
- Ensure the key actually exists (use contains/keys to check first).
- Rename the key in the lookup to match the current map.
Example fix
// before name = lookup(var.tags, "Name") // after name = lookup(var.tags, "Name", "default-name")
Defensive patterns
Strategy: validation
Validate before calling
// Always provide a default, or check contains() first.
locals {
name = contains(keys(var.map), var.key) ? lookup(var.map, var.key) : "fallback"
} Prevention
- Supply a third default argument to lookup() when the key may be absent.
- Use contains(keys(map), key) for explicit presence checks.
- Keep map keys stable across refactors; update callers when renaming.
When it happens
Trigger: lookup(map, key) where key does not exist in map and the third (default) argument is omitted; or lookup on an object whose attribute name is missing.
Common situations: Map keys sourced from a variable/list where a specific key isn't always present, environment-specific data missing an entry, refactor that renamed a key but callers weren't updated.
Related errors
- lookup() takes two or three arguments, got
- invalid CIDR expression
- at most 1 action can be invoked per operation
- can't compute sum of opposing infinities
- can't delete default state
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/6cd297cf2ecf08a7.
Report an issue: GitHub.
Appendix: source
Thrown at internal/lang/funcs/collection.go:326
lookupKey := keyVal.AsString()
if mapVar.Type().IsObjectType() {
if mapVar.Type().HasAttribute(lookupKey) {
return mapVar.GetAttr(lookupKey).WithMarks(markses...), nil
}
} else if mapVar.HasIndex(cty.StringVal(lookupKey)) == cty.True {
return mapVar.Index(cty.StringVal(lookupKey)).WithMarks(markses...), nil
}
if defaultValueSet {
defaultVal, err = convert.Convert(defaultVal, retType)
if err != nil {
return cty.NilVal, err
}
return defaultVal.WithMarks(markses...), nil
}
return cty.UnknownVal(cty.DynamicPseudoType), fmt.Errorf(
"lookup failed to find key %s", redactIfSensitive(lookupKey, keyMarks))
},
})
// MatchkeysFunc constructs a function that constructs a new list by taking a
// subset of elements from one list whose indexes match the corresponding
// indexes of values in another list.
var MatchkeysFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "values",
Type: cty.List(cty.DynamicPseudoType),
},
{
Name: "keys",
Type: cty.List(cty.DynamicPseudoType),
},
{View on GitHub (pinned to d32a084675)