hashicorp/terraform · error

Failed to get existing workspaces: %s

Error message

Failed to get existing workspaces: %s

What it means

Returned by selectWorkspace() when b.Workspaces() returns diagnostics containing errors other than the well-known ErrWorkspacesNotSupported sentinel. selectWorkspace needs the workspace list to validate the current selection or prompt the user, so any other backend error is fatal. The %s is the diagnostics error text.

Source

Thrown at internal/command/meta_backend.go:246

		m.backendConfigState = &workdir.BackendConfigState{
			Type:      "local",
			ConfigRaw: json.RawMessage("{}"),
		}
	}

	return local, diags
}

// selectWorkspace gets a list of existing workspaces and then checks
// if the currently selected workspace is valid. If not, it will ask
// the user to select a workspace from the list.
func (m *Meta) selectWorkspace(b backend.Backend) error {
	workspaces, diags := b.Workspaces()
	if diags.HasErrors() && diags.Err().Error() == backend.ErrWorkspacesNotSupported.Error() {
		return nil
	}
	if diags.HasErrors() {
		return fmt.Errorf("Failed to get existing workspaces: %s", diags.Err())
	}
	if diags.HasWarnings() {
		log.Printf("[WARN] selectWorkspace: warning(s) returned when getting workspaces: %s", diags.ErrWithWarnings())
	}
	if len(workspaces) == 0 {
		if c, ok := b.(*cloud.Cloud); ok && m.input {
			// len is always 1 if using Name; 0 means we're using Tags and there
			// aren't any matching workspaces. Which might be normal and fine, so
			// let's just ask:
			name, err := m.UIInput().Input(context.Background(), &terraform.InputOpts{
				Id:          "create-workspace",
				Query:       "\n[reset][bold][yellow]No workspaces found.[reset]",
				Description: fmt.Sprintf(inputCloudInitCreateWorkspace, c.WorkspaceMapping.DescribeTags()),
			})
			if err != nil {
				return fmt.Errorf("Couldn't create initial workspace: %w", err)
			}
			name = strings.TrimSpace(name)

View on GitHub (pinned to c9def3e214)

Solutions

  1. Check the wrapped error text — for cloud backends it usually indicates auth (401/403) or connectivity.
  2. Verify credentials: `terraform login` or set the correct TF_TOKEN_* / AWS_* / GOOGLE_* env vars for your backend.
  3. Ensure the principal has list/read permissions on the state workspace prefix/path.
  4. Retry transient API failures; for persistent failures inspect backend-specific logs.

Example fix

# before: missing cloud token
terraform init
# Failed to get existing workspaces: ...
# after
terraform login
terraform init
Defensive patterns

Strategy: retry

Validate before calling

// Pre-flight: ensure backend can list workspaces
if ws, d := b.Workspaces(); d.HasErrors() && d.Err().Error() != backend.ErrWorkspacesNotSupported.Error() {
    return fmt.Errorf("fix backend workspace access first: %s", d.Err())
} else {
    _ = ws
}

Prevention

When it happens

Trigger: A backend that supports workspaces returns an error from its Workspaces() method during the workspace selection phase of backend init — e.g. a cloud/remote backend whose API call to list workspaces failed (auth, 5xx, network), or a state backend with a corrupted workspace index.

Common situations: HCP Terraform / cloud backend with an expired or revoked API token; a remote HTTP backend whose endpoint is down; S3 backend with insufficient IAM permissions to list workspaces (ListObjectsV2); transient network failure reaching the state storage API.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/3f3ae4087e79f4fc. Report an issue: GitHub.