hashicorp/terraform · error

Error loading the state: %[1]s Please ensure that your…

Error message

Error loading the state: %[1]s

Please ensure that your Terraform state exists and that you've
configured it properly. You can use the "-state" flag to point
Terraform at another state file.

What it means

Thrown by state_show.go when b.StateMgr(env) returns diagnostics with errors. This is the `terraform state show` analogue of [740]: the backend cannot construct a state manager for the selected workspace. The message is multi-line guidance pointing the user to the `-state` flag. The wrapped error is sDiags.Err() via the errStateLoadingState template.

Solutions

  1. Run `terraform init` to validate and persist the backend configuration.
  2. Verify the workspace: `terraform workspace list` / `terraform workspace select`.
  3. If you intend a local state file, pass `-state=<path>` explicitly.
  4. Refresh credentials (AWS_PROFILE, GOOGLE_APPLICATION_CREDENTIALS, ARM_USE_MSI=true, etc.).

Example fix

# before
 terraform state show aws_instance.web   # no backend / wrong ws

# after
 terraform init
 terraform workspace select prod
 terraform state show aws_instance.web
Defensive patterns

Strategy: validation

Validate before calling

// Confirm workspace exists before StateMgr.
if _, err := c.Workspace(); err != nil { return err }
if !workspaceExists(b, workspace) {
    return fmt.Errorf("workspace %q not found on backend; create it with 'terraform workspace new'", workspace)
}

Try / catch

stateMgr, sDiags := b.StateMgr(env)
if sDiags.HasErrors() {
    diags = diags.Append(fmt.Errorf(errStateLoadingState, sDiags.Err()))
    return view.DisplayResourceInstanceState(jsonformat.State{}, diags)
}

Prevention

When it happens

Trigger: `terraform state show <addr>` against a backend whose StateMgr fails: bad workspace, auth failure, missing remote state object metadata, locked backend config from a partial init.

Common situations: Workspace does not exist on remote backend; credentials expired; backend block was edited but `terraform init` not re-run; running `state show` before any `init`/`apply` has created state; pointing at a remote backend in a dir that has no backend config.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/6fc0d6c78f31f57a. Report an issue: GitHub.

Appendix: source

Thrown at internal/command/state_show.go:114

		return view.DisplayResourceInstanceState(jsonformat.State{}, diags)
	}

	// Get the schemas from the context
	schemas, diags := lr.Core.Schemas(lr.Config, lr.InputState)
	if diags.HasErrors() {
		return view.DisplayResourceInstanceState(jsonformat.State{}, diags)
	}

	// Get the state
	env, err := c.Workspace()
	if err != nil {
		diags = diags.Append(fmt.Sprintf("Error selecting workspace: %s\n", err))
		view.Diagnostics(diags)
		return 1
	}
	stateMgr, sDiags := b.StateMgr(env)
	if sDiags.HasErrors() {
		diags = diags.Append(fmt.Errorf(errStateLoadingState, sDiags.Err()))
		return view.DisplayResourceInstanceState(jsonformat.State{}, diags)
	}
	if err := stateMgr.RefreshState(); err != nil {
		diags = diags.Append(fmt.Errorf("Failed to refresh state: %s\n", err))
		return view.DisplayResourceInstanceState(jsonformat.State{}, diags)
	}

	state := stateMgr.State()
	if state == nil {
		diags = diags.Append(errors.New(errStateNotFound))
		return view.DisplayResourceInstanceState(jsonformat.State{}, diags)
	}

	is := state.ResourceInstance(addr)
	if !is.HasCurrent() {
		diags = diags.Append(errors.New(errNoInstanceFound))
		return view.DisplayResourceInstanceState(jsonformat.State{}, diags)
	}

View on GitHub (pinned to d32a084675)