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 StateShowCommand (state_show.go:124) using the shared errStateNotFound constant. After refreshing state via stateMgr.RefreshState(), state := stateMgr.State() returns nil — there is no state file at all (local backend with no terraform.tfstate, or a remote backend returning empty). The message tells the operator state commands need a state file.
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 c9def3e214)
Solutions
- Run `terraform apply` (or `terraform refresh`) first to create/refresh state.
- Verify the workspace: `terraform workspace list` and switch to the one holding state (`terraform workspace select <name>`).
- If using a custom location, pass `-state=<path>` pointing at an existing tfstate file, or fix the backend config and `terraform init`.
Example fix
# before terraform state show aws_instance.web # No state file was found # after terraform apply # creates state terraform state show aws_instance.web
Defensive patterns
Strategy: validation
Validate before calling
// Before running state commands, confirm a state file exists.
if _, err := os.Stat("terraform.tfstate"); os.IsNotExist(err) {
return errors.New("no state file; run terraform apply first")
} Try / catch
out, err := exec.Command("terraform", "state", "show", addr).CombinedOutput()
if err != nil && bytes.Contains(out, []byte("No state file was found")) {
// run apply/refresh to create state, then retry
} Prevention
- Verify the workspace holds state (`terraform state list`) before running `state show`.
- Run apply/refresh to materialize state before inspecting it.
When it happens
Trigger: Running `terraform state show <addr>` (or other state commands) in a directory where no state exists: never applied, .terraform/terraform.tfstate deleted, or the backend returned no state for the workspace.
Common situations: Fresh project never applied; wrong workspace selected so the backend has no state; -state flag pointing at a non-existent path; remote backend auth/network issue returned empty.
Related errors
- Error loading the state: %[1]s Please ensure that your Terr
- No instance found for the given address! This command requi
- error loading state: %w
- can't delete default state
- Error loading state: %s
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/c3b51ad7fb787d78.
Report an issue: GitHub.