hashicorp/terraform · error

Couldn't create initial workspace

Error message

Couldn't create initial workspace: %w

What it means

Returned from `Meta.selectWorkspace` when the backend is `*cloud.Cloud`, no workspaces matched, interactive input is enabled, and `m.UIInput().Input` (the prompt asking the user to name a new workspace) itself returns an error. The `%w` wrap preserves the original error for `errors.Is`/`errors.As` unwrapping. This is an input-mechanism failure, not a workspace problem.

Solutions

  1. Pre-create the workspace in HCP Terraform so the prompt is not triggered, then re-run.
  2. Set `workspaces.name = "<explicit>"` in the `cloud` block to avoid the 'No workspaces found' path.
  3. Run in a real TTY so the interactive prompt can be answered.
  4. If automating, set `-input=false` and specify the workspace explicitly (the cloud branch requires `m.input`).

Example fix

// before: zero workspaces + non-interactive -> prompt fails
cloud { organization = "acme" workspaces { tags = ["env:prod"] } }
// after: name the workspace explicitly
cloud { organization = "acme" workspaces { name = "prod" } }
terraform init
Defensive patterns

Strategy: validation

Validate before calling

// Avoid the create-workspace prompt by ensuring >=1 workspace matches,
// or by setting workspaces.name explicitly.
if nonInteractive && len(workspaces) == 0 {
    return errors.New("no workspaces and input disabled; set cloud.workspaces.name or pre-create a workspace")
}

Type guard

null

Try / catch

// UIInput errors during the create prompt are not retryable; pre-create the workspace.
if err != nil {
    return fmt.Errorf("Couldn't create initial workspace: %w", err)
}

Prevention

When it happens

Trigger: `UIInput().Input` fails during the 'No workspaces found' prompt — stdin is closed, the terminal is not a TTY, an interrupt occurs, or the UIInput implementation errors internally. The cloud branch is entered only when `len(workspaces) == 0` and `m.input` is true.

Common situations: First `terraform` run against a brand-new HCP Terraform organization with zero workspaces, in a non-interactive context (CI, container) where the create prompt cannot be answered; an interrupt during the prompt; stdin redirected from a closed pipe.

Related errors


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

Appendix: source

Thrown at internal/command/meta_backend.go:262

	}
	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)
			if name == "" {
				return fmt.Errorf("Couldn't create initial workspace: no name provided")
			}
			log.Printf("[TRACE] Meta.selectWorkspace: selecting the new HCP Terraform workspace requested by the user (%s)", name)
			return m.SetWorkspace(name)
		} else {
			return &errBackendNoExistingWorkspaces{}
		}
	}

	// Get the currently selected workspace.
	workspace, err := m.Workspace()
	if err != nil {
		return err
	}

View on GitHub (pinned to d32a084675)