{"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/c9def3e214014c1188faabfc4a5bde5095139765/internal/lang/funcs/collection.go#L308-L344","documentation":"Returned by the lookup built-in's Impl (LookupFunc, internal/lang/funcs/collection.go:326) when the requested key is absent from the map/object and no default value (third argument) was supplied. After unmarking and confirming both map and key are known, the function checks HasAttribute (objects) / HasIndex (maps); on miss with no default set it returns cty.UnknownVal(DynamicPseudoType) plus this error, with the key redacted if it carries sensitive marks.","triggerScenarios":"Calling lookup(var.some_map, \"missing_key\") where 'missing_key' is not present and no third default argument is given; both map and key are fully known so lookup falls through to the not-found path.","commonSituations":"Referencing a tag/label key that doesn't exist in the map, environment-specific maps missing a key in some workspaces, typo'd keys, or assuming a key exists when the source only populates it conditionally.","solutions":["Supply a default: lookup(var.some_map, \"missing_key\", \"fallback\").","Use try() to fall back gracefully: try(var.some_map.missing_key, \"fallback\").","Verify the key exists with contains(keys(var.some_map), \"missing_key\") before lookup.","Populate the map upstream so the expected key is always present."],"exampleFix":"# before (HCL)\nv = lookup(var.tags, \"env\")  # tags has no \"env\" -> lookup failed to find key \"env\"\n\n# after\nv = lookup(var.tags, \"env\", \"default\")\n# or\nv = try(var.tags.env, \"default\")\n# or guard:\nv = contains(keys(var.tags), \"env\") ? lookup(var.tags, \"env\") : \"default","handlingStrategy":"fallback","validationCode":"# (HCL) check existence before lookup\nlocals {\n  has_key = contains(keys(var.tags), \"env\")\n  v       = local.has_key ? lookup(var.tags, \"env\") : \"default\"\n}","typeGuard":"# (HCL) type-ish guard using contains\nlocals {\n  key_present = contains(keys(var.tags), \"env\")\n}","tryCatchPattern":"# (HCL) always supply a default, or use try()\nvalue = lookup(var.tags, \"env\", \"default\")\n# or\nvalue = try(var.tags.env, \"default\")","preventionTips":["Always pass a default to lookup for optional keys.","Prefer try() for graceful attribute access on maps/objects.","Use contains(keys(...), k) to check existence first.","Populate maps upstream so expected keys always exist."],"tags":["lookup","hcl-function","map","validation","key-not-found"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}