{"record":{"id":"04161508635e5b78","repo":"hashicorp/terraform","slug":"error-refreshing-state-s","errorCode":null,"errorMessage":"Error refreshing state: %s","messagePattern":"Error refreshing state: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/command/init_run.go","lineNumber":328,"sourceCode":"\t// on a previous run) we'll use the current state as a potential source\n\t// of provider dependencies.\n\tif back != nil {\n\t\tc.ignoreRemoteVersionConflict(back)\n\t\tworkspace, err := c.Workspace()\n\t\tif err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error selecting workspace: %s\", err))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\t\tsMgr, sDiags := back.StateMgr(workspace)\n\t\tif sDiags.HasErrors() {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error loading state: %s\", sDiags.Err()))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\tif err := sMgr.RefreshState(); err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error refreshing state: %s\", err))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\tstate = sMgr.State()\n\t}\n\n\tif initArgs.Get {\n\t\tmodsOutput, modsAbort, modsDiags := c.getModules(ctx, path, initArgs.TestsDirectory, rootModEarly, initArgs.Upgrade, view, policyClient)\n\t\tdiags = diags.Append(modsDiags)\n\t\tif modsAbort || modsDiags.HasErrors() {\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\t\tif modsOutput {\n\t\t\t// If we outputted information, then we need to output a newline\n\t\t\t// so that our success message is nicely spaced out from prior text.\n\t\t\tview.Spacer()","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/init_run.go#L310-L346","documentation":"Thrown when sMgr.RefreshState() fails during init after a state manager was successfully constructed. RefreshState contacts the backend to pull the latest state; failures are network/auth/I/O errors against the remote state store.","triggerScenarios":"The state manager is built but refreshing state from the backend fails: S3 GET on the state key fails (404/403), DynamoDB lock acquisition fails, HTTP backend returns 5xx, or network timeout.","commonSituations":"State object deleted out-of-band; lock table missing; transient network error; credential expiry between StateMgr construction and refresh; concurrent state operation holding the lock.","solutions":["Verify the remote state object exists and is accessible from the configured backend.","Check the lock table (DynamoDB) exists and is not held by another run.","Retry on transient network errors; refresh credentials if expired."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Confirm the state object exists before init refresh (S3 example).\nfunc stateObjectExists(bucket, key string) bool {\n    _, err := s3.HeadObject(bucket, key)\n    return err == nil\n}","typeGuard":null,"tryCatchPattern":"// Retry transient refresh failures with backoff.\nfor i := 0; i < 3; i++ {\n    if err := sMgr.RefreshState(); err == nil { break }\n    time.Sleep(backoff(i))\n}","preventionTips":["Verify the state object exists and is accessible.","Ensure the lock table exists and is not held by another run.","Retry transient network errors; refresh credentials if expired."],"tags":["terraform-init","backend","state","refresh","remote-state"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}