hashicorp/terraform · warning

Couldn't create initial workspace: %w

Error message

Couldn't create initial workspace: %w

What it means

Returned by selectWorkspace() in the HCP Terraform / cloud code path when, having found zero workspaces, it interactively prompts the user to name a new workspace and UIInput().Input() itself returns an error. The %w wraps the underlying input I/O error. This only triggers for cloud backends using tag-based workspace mapping (len(workspaces)==0) in interactive mode.

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 c9def3e214)

Solutions

  1. Provide the workspace name non-interactively: set TF_WORKSPACE or pre-create the workspace in the HCP Terraform UI/API.
  2. Run with -input=false to avoid the interactive prompt entirely (and pre-configure the workspace mapping).
  3. Ensure stdin is a functional TTY when running interactively.

Example fix

// before: cloud + tags, no workspace, closed stdin
terraform init
// after: pre-create or name the workspace
terraform workspace new prod
terraform init
Defensive patterns

Strategy: validation

Validate before calling

// For cloud tag-based mapping, pre-create or name a workspace to avoid the prompt
if name := os.Getenv("TF_WORKSPACE"); name == "" && !m.Input() {
    return errors.New("cloud tag mapping with no workspaces requires TF_WORKSPACE or interactive input")
}

Prevention

When it happens

Trigger: The cloud backend returns no workspaces, m.input is true, the interactive 'create-workspace' prompt is issued, and the UIInput reader fails with an error (EOF, broken pipe, read error).

Common situations: A cloud workspace with tag-based mapping where the initial interactive prompt is issued but stdin is non-functional (CI without -input=false, closed pipe, scripted run that closes stdin early).

Related errors


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