{"record":{"id":"6cd297cf2ecf08a7","repo":"hashicorp/terraform","slug":"lookup-failed-to-find-key-s","errorCode":null,"errorMessage":"lookup failed to find key %s","messagePattern":"lookup failed to find key (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lang/funcs/collection.go","lineNumber":326,"sourceCode":"\t\tlookupKey := keyVal.AsString()\n\n\t\tif mapVar.Type().IsObjectType() {\n\t\t\tif mapVar.Type().HasAttribute(lookupKey) {\n\t\t\t\treturn mapVar.GetAttr(lookupKey).WithMarks(markses...), nil\n\t\t\t}\n\t\t} else if mapVar.HasIndex(cty.StringVal(lookupKey)) == cty.True {\n\t\t\treturn mapVar.Index(cty.StringVal(lookupKey)).WithMarks(markses...), nil\n\t\t}\n\n\t\tif defaultValueSet {\n\t\t\tdefaultVal, err = convert.Convert(defaultVal, retType)\n\t\t\tif err != nil {\n\t\t\t\treturn cty.NilVal, err\n\t\t\t}\n\t\t\treturn defaultVal.WithMarks(markses...), nil\n\t\t}\n\n\t\treturn cty.UnknownVal(cty.DynamicPseudoType), fmt.Errorf(\n\t\t\t\"lookup failed to find key %s\", redactIfSensitive(lookupKey, keyMarks))\n\t},\n})\n\n// MatchkeysFunc constructs a function that constructs a new list by taking a\n// subset of elements from one list whose indexes match the corresponding\n// indexes of values in another list.\nvar MatchkeysFunc = function.New(&function.Spec{\n\tParams: []function.Parameter{\n\t\t{\n\t\t\tName: \"values\",\n\t\t\tType: cty.List(cty.DynamicPseudoType),\n\t\t},\n\t\t{\n\t\t\tName: \"keys\",\n\t\t\tType: cty.List(cty.DynamicPseudoType),\n\t\t},\n\t\t{","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/lang/funcs/collection.go#L308-L344","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nname = lookup(var.tags, \"Name\")\n\n// after\nname = lookup(var.tags, \"Name\", \"default-name\")","handlingStrategy":"validation","validationCode":"// Always provide a default, or check contains() first.\nlocals {\n  name = contains(keys(var.map), var.key) ? lookup(var.map, var.key) : \"fallback\"\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["terraform","hcl-functions","lookup","missing-key","validation"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}