{"record":{"id":"a71e12435e1ae6dc","repo":"hashicorp/terraform","slug":"lookup-takes-two-or-three-arguments-got-d","errorCode":null,"errorMessage":"lookup() takes two or three arguments, got %d","messagePattern":"lookup\\(\\) takes two or three arguments, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lang/funcs/collection.go","lineNumber":248,"sourceCode":"\t\t},\n\t\t{\n\t\t\tName:         \"key\",\n\t\t\tType:         cty.String,\n\t\t\tAllowMarked:  true,\n\t\t\tAllowUnknown: true,\n\t\t},\n\t},\n\tVarParam: &function.Parameter{\n\t\tName:             \"default\",\n\t\tType:             cty.DynamicPseudoType,\n\t\tAllowUnknown:     true,\n\t\tAllowDynamicType: true,\n\t\tAllowNull:        true,\n\t\tAllowMarked:      true,\n\t},\n\tType: func(args []cty.Value) (ret cty.Type, err error) {\n\t\tif len(args) < 1 || len(args) > 3 {\n\t\t\treturn cty.NilType, fmt.Errorf(\"lookup() takes two or three arguments, got %d\", len(args))\n\t\t}\n\n\t\tty := args[0].Type()\n\n\t\tswitch {\n\t\tcase ty.IsObjectType():\n\t\t\tif !args[1].IsKnown() {\n\t\t\t\treturn cty.DynamicPseudoType, nil\n\t\t\t}\n\n\t\t\tkeyVal, _ := args[1].Unmark()\n\t\t\tkey := keyVal.AsString()\n\t\t\tif ty.HasAttribute(key) {\n\t\t\t\treturn args[0].GetAttr(key).Type(), nil\n\t\t\t} else if len(args) == 3 {\n\t\t\t\t// if the key isn't found but a default is provided,\n\t\t\t\t// return the default type\n\t\t\t\treturn args[2].Type(), nil","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/lang/funcs/collection.go#L230-L266","documentation":"Returned by the lookup built-in's Type function (LookupFunc, internal/lang/funcs/collection.go:248) when the number of arguments is outside the accepted 2-or-3 range. lookup takes inputMap + key (2 params) plus an optional default (variadic); passing more than one default value (4+ total args) violates the Type check len(args) > 3 and yields this arity error before any value work happens.","triggerScenarios":"Calling lookup(map, key, default, extra) (4 args) or otherwise passing more than the allowed set; the variadic default parameter collects surplus args beyond the two fixed params, and len(args) exceeds 3.","commonSituations":"Trying to supply multiple fallback values, misunderstanding lookup's signature as varargs, copy-paste adding an extra argument, or templating that injects an additional positional arg.","solutions":["Pass exactly 2 or 3 arguments: lookup(map, key) or lookup(map, key, default).","For multiple fallbacks, nest lookups or use coalesce/try: coalesce(lookup(m,\"a\"), lookup(m,\"b\"), \"default\").","Audit generated/templated lookup calls for stray extra arguments.","Prefer try() when you need graceful multi-key/default handling."],"exampleFix":"# before (HCL)\nv = lookup(var.tags, \"env\", \"dev\", \"prod\")  # 4 args -> lookup() takes two or three arguments, got 4\n\n# after\nv = lookup(var.tags, \"env\", \"dev\")\n# multiple fallbacks:\nv = coalesce(lookup(var.tags, \"env\"), lookup(var.tags, \"environment\"), \"dev\")","handlingStrategy":"validation","validationCode":"# (HCL) enforce the 2-or-3 arg contract in a wrapper\nlocals {\n  v = length([var.m, \"k\", \"d\"]) > 3 ? null : lookup(var.m, \"k\", \"d\")\n}","typeGuard":"// (Go) true if lookup arg count is valid\nfunc validLookupArity(n int) bool { return n == 2 || n == 3 }","tryCatchPattern":"# (HCL) use try/coalesce instead of extra positional defaults\nvalue = coalesce(lookup(var.m, \"a\", \"\"), lookup(var.m, \"b\", \"\"), \"default\")","preventionTips":["Pass exactly 2 or 3 args to lookup.","Use coalesce()/try() for multiple fallbacks, not extra positional args.","Lint generated lookup calls for stray arguments.","Document the lookup signature in module readmes."],"tags":["lookup","hcl-function","map","validation","arity"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}