hashicorp/terraform · error
No state file was found! State management commands require…
Error message
No state file was found! State management commands require a state file. Run this command in a directory where Terraform has been run or use the -state flag to point the command to a specific state location.
What it means
Returned by `terraform state show` when, after refreshing the backend state manager, `stateMgr.State()` returns nil — i.e. there is no state file at all for the current workspace. State-management commands require an existing state, so this is a hard stop with guidance to run in an initialized directory or point `-state` at a file.
Solutions
- Run `terraform apply` first to create initial state, then `terraform state show`.
- Verify the workspace with `terraform workspace list`/`show` and switch to the one that holds the resource.
- Check the `-state` flag path or the backend configuration (e.g. S3 bucket/key) is correct.
Example fix
# before terraform state show aws_instance.web # fresh dir -> error # after terraform apply && terraform state show aws_instance.web
Defensive patterns
Strategy: validation
Validate before calling
// Mirror the check before operating on state:
state := stateMgr.State()
if state == nil {
return errors.New(errStateNotFound)
} Prevention
- Confirm an apply has created state before running state commands.
- Verify the workspace and `-state` path before `terraform state show`.
- In automation, check `terraform state list` exit status before deeper queries.
When it happens
Trigger: `stateMgr.RefreshState()` succeeds but `state == nil`: the configured backend has no state object (empty workspace, never-applied directory, or `-state` points at a non-existent path).
Common situations: Running `terraform state show <addr>` in a fresh project that has never been applied; wrong workspace selected; `-state=...` path is wrong; backend state object was deleted out-of-band.
Related errors
- backendDiags.Err()
- No instance found for the given address! This command…
- Can't ask approval for state migration when interactive…
- errInteractiveInputDisabled
- Error inspecting states in the
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/c3b51ad7fb787d78.
Report an issue: GitHub.
Appendix: source
Thrown at internal/command/state_show.go:124
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)
}
// check if the resource has a configured provider, otherwise this will use the default provider
rs := state.Resource(addr.ContainingResource())
absPc := addrs.AbsProviderConfig{
Provider: rs.ProviderConfig.Provider,
Alias: rs.ProviderConfig.Alias,
Module: addrs.RootModule,
}
singleInstance := states.NewState()
singleInstance.EnsureModule(addr.Module).SetResourceInstanceCurrent(View on GitHub (pinned to d32a084675)