{"record":{"id":"e0d754bedc45732b","repo":"hashicorp/terraform","slug":"resource-not-found-in-state","errorCode":null,"errorMessage":"resource not found in state","messagePattern":"resource not found in state","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/command/views/show.go","lineNumber":252,"sourceCode":"\n\tif diags.HasErrors() {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\nfunc findResourceInChildModules(mod jsonstate.Module) jsonstate.Resource {\n\tfor _, cm := range mod.ChildModules {\n\t\tif len(cm.Resources) == 1 {\n\t\t\treturn cm.Resources[0]\n\t\t}\n\t}\n\tfor _, child := range mod.ChildModules {\n\t\treturn findResourceInChildModules(child)\n\t}\n\t// this shouldn't be possible; we would have returned an error earlier if\n\t// the resource wasn't found.\n\tpanic(\"resource not found in state\")\n}\n","sourceCodeStart":234,"sourceCodeEnd":254,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/views/show.go#L234-L254","documentation":"findResourceInChildModules panics with 'resource not found in state' when ShowJSON.DisplayResourceInstanceState (internal/command/views/show.go:241) cannot locate the single resource it expects. It first scans each child module for exactly one resource, then recurses; if no branch yields a resource, it panics. The comment notes this should be unreachable because earlier validation should have rejected a missing resource.","triggerScenarios":"terraform state show <addr> where the state is non-empty (jsonState.Empty()==false), the root module has zero resources, and no descendant module at any depth holds exactly one resource (e.g. a module with 0 or 2+ resources at every level, or all child modules empty).","commonSituations":"State left behind by count=0/for_each-over-empty resources, a state file migrated or edited so the addressed resource sits in a module path the walker does not match, a race where state changed between address resolution and rendering, or a regression in the upstream guard that was supposed to return a diagnostic before reaching the view.","solutions":["Run 'terraform state list' to confirm the exact resource address and module path; re-run state show with the fully-qualified address returned there.","If the address is valid but the resource is gone, 'terraform refresh'/'terraform apply -refresh-only' to reconcile state with reality, or restore a known-good state.","Inspect the state with 'terraform state pull' to verify the module tree actually contains the expected resource instance.","Report upstream: the panic means the earlier 'resource not found' diagnostic guard was bypassed, so include the state structure and the command that triggered it."],"exampleFix":"// before\nfor _, child := range mod.ChildModules {\n    return findResourceInChildModules(child)\n}\npanic(\"resource not found in state\")\n\n// after (return a zero value / signal to caller instead of panicking)\nfor _, child := range mod.ChildModules {\n    if r, ok := findResourceInChildModulesOk(child); ok {\n        return r, true\n    }\n}\nreturn jsonstate.Resource{}, false","handlingStrategy":"validation","validationCode":"// Before delegating to ShowJSON.DisplayResourceInstanceState, confirm the\n// resource actually exists in the module tree.\nfunc resourceExists(mod jsonstate.Module, addr string) bool {\n    for _, r := range mod.Resources {\n        if matchAddr(r, addr) {\n            return true\n        }\n    }\n    for _, cm := range mod.ChildModules {\n        if resourceExists(cm, addr) {\n            return true\n        }\n    }\n    return false\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        if msg, ok := r.(string); ok && strings.Contains(msg, \"resource not found in state\") {\n            // surface a normal diagnostic instead of an uncaught panic\n            diags = diags.Append(tfdiags.Sourceless(tfdiags.Error,\n                \"Resource not found\", \"The requested resource is not present in the current state.\"))\n            return\n        }\n        panic(r)\n    }\n}()","preventionTips":["Always resolve the exact address via 'terraform state list' before calling 'terraform state show'.","Run 'terraform apply -refresh-only' when state and reality drift so addresses stay valid.","Never hand-edit state to delete a resource without also updating the module tree."],"tags":["panic","state-show","state","recursion","defensive","module-tree"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}