{"record":{"id":"1a67f6a8564ce3aa","repo":"hashicorp/terraform","slug":"newinstanceinfo-cannot-convert-module-instance-wit","errorCode":null,"errorMessage":"NewInstanceInfo cannot convert module instance with key","messagePattern":"NewInstanceInfo cannot convert module instance with key","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/legacy/terraform/resource.go","lineNumber":62,"sourceCode":"//\n// InstanceInfo is a legacy type, and uses of it should be gradually replaced\n// by direct use of addrs.AbsResource or addrs.AbsResourceInstance as\n// appropriate.\n//\n// The legacy InstanceInfo type cannot represent module instances with instance\n// keys, so this function will panic if given such a path. Uses of this type\n// should all be removed or replaced before implementing \"count\" and \"for_each\"\n// arguments on modules in order to avoid such panics.\n//\n// This legacy type also cannot represent resource instances with string\n// instance keys. It will panic if the given key is not either NoKey or an\n// IntKey.\nfunc NewInstanceInfo(addr addrs.AbsResourceInstance) *InstanceInfo {\n\t// We need an old-style []string module path for InstanceInfo.\n\tpath := make([]string, len(addr.Module))\n\tfor i, step := range addr.Module {\n\t\tif step.InstanceKey != addrs.NoKey {\n\t\t\tpanic(\"NewInstanceInfo cannot convert module instance with key\")\n\t\t}\n\t\tpath[i] = step.Name\n\t}\n\n\t// This is a funny old meaning of \"id\" that is no longer current. It should\n\t// not be used for anything users might see. Note that it does not include\n\t// a representation of the resource mode, and so it's impossible to\n\t// determine from an InstanceInfo alone whether it is a managed or data\n\t// resource that is being referred to.\n\tid := fmt.Sprintf(\"%s.%s\", addr.Resource.Resource.Type, addr.Resource.Resource.Name)\n\tif addr.Resource.Resource.Mode == addrs.DataResourceMode {\n\t\tid = \"data.\" + id\n\t}\n\tif addr.Resource.Key != addrs.NoKey {\n\t\tswitch k := addr.Resource.Key.(type) {\n\t\tcase addrs.IntKey:\n\t\t\tid = id + fmt.Sprintf(\".%d\", int(k))\n\t\tdefault:","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/legacy/terraform/resource.go#L44-L80","documentation":"Panics by design in NewInstanceInfo (resource.go:62). The legacy InstanceInfo type cannot represent module instances created with count/for_each; if any module path step has a non-NoKey instance key, it panics. The docstring explicitly warns this path must be resolved before module count/for_each is supported.","triggerScenarios":"A provider using the legacy helper/schema ResourceData path is exercised against a module instance created with `count` or `for_each` on a module block, routing through NewInstanceInfo.","commonSituations":"Older SDK-based providers run under Terraform configurations that apply count/for_each to module blocks; SDK version lag.","solutions":["Upgrade the provider to a build against a newer SDK that no longer routes through legacy InstanceInfo.","Avoid count/for_each on the module blocks that exercise the affected legacy provider path.","Restructure: move the resource out of the counted module, or replicate the module manually."],"exampleFix":"// before\nmodule \"svc\" { count = var.n, source = \"./svc\" }\n// after\nmodule \"svc_a\" { source = \"./svc\" }\nmodule \"svc_b\" { source = \"./svc\" }","handlingStrategy":"validation","validationCode":"func noModuleInstanceKeys(addr addrs.AbsResourceInstance) error {\n    for _, step := range addr.Module {\n        if step.InstanceKey != addrs.NoKey {\n            return fmt.Errorf(\"module %s has instance key; legacy InstanceInfo unsupported\", step)\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid count/for_each on modules that exercise legacy provider paths.","Upgrade providers to newer SDK builds.","Prefer modern addrs types over legacy InstanceInfo."],"tags":["terraform","module","instance-key","legacy","panic"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}