pulumi/pulumi · error

could not create stack: %w

Error message

could not create stack: %w

What it means

Generic wrapper for backend.CreateStack failures inside CreateStack. Specific error types (StackAlreadyExistsError, OverStackLimitError) are returned unwrapped so callers can type-assert them; every other backend failure (auth, network, validation, organization policy) is wrapped with this message.

Source

Thrown at pkg/cmd/pulumi/stack/io.go:438

		if !found {
			return nil, errors.New("could not get project from stack reference")
		}
		escEnvironment = proj.String() + "/" + stackRef.Name().String()
		backendOpts.Config = &apitype.StackConfig{
			Environment: escEnvironment,
		}
	}

	stack, err := b.CreateStack(ctx, stackRef, root, initialState, &backendOpts)
	if err != nil {
		// If it's a well-known error, don't wrap it.
		if _, ok := err.(*backenderr.StackAlreadyExistsError); ok {
			return nil, err
		}
		if _, ok := err.(*backenderr.OverStackLimitError); ok {
			return nil, err
		}
		return nil, fmt.Errorf("could not create stack: %w", err)
	}

	if !opts.Quiet {
		sink.Infof(diag.Message("", "Created stack '%s'"), stack.Ref())
	}

	if escEnvironment != "" {
		// Shared helper without a *cobra.Command writer; uses process stdout.
		fmt.Printf("Created environment %s for stack configuration\n", escEnvironment) //nolint:forbidigo
	}

	// Now that we've created the stack, we'll write out any necessary configuration changes.
	if needsSave {
		err = SaveProjectStack(ctx, stack, ps, opts.ConfigFile)
		if err != nil {
			return nil, fmt.Errorf("%w: %w", ErrSaveStackConfig, err)
		}
	}

View on GitHub (pinned to 793f7b2e16)

Solutions

  1. Read the wrapped cause (%w) to identify the backend error and fix it (login, naming, permissions)
  2. Check whether the failure is actually duplicate-exists or over-limit, which surface as their own typed errors
  3. Re-authenticate with `pulumi login` / valid PULUMI_ACCESS_TOKEN
  4. Retry if the wrapped error indicates a transient 5xx/network condition

Example fix

// before
$ pulumi stack init prod
error: could not create stack: 401 unauthorized
// after
$ pulumi login
$ pulumi stack init prod
Defensive patterns

Strategy: type-guard

Validate before calling

// pre-validate name and auth
if err := exec.Command("pulumi", "whoami").Run(); err != nil {
    return errors.New("login required before stack init")
}

Type guard

var existsErr *backenderr.StackAlreadyExistsError
var limitErr *backenderr.OverStackLimitError
if errors.As(err, &existsErr) { /* already exists */ }
else if errors.As(err, &limitErr) { /* over limit */ }
else { /* wrapped: could not create stack: %v */ }

Try / catch

var existsErr *backenderr.StackAlreadyExistsError
if err != nil {
    if errors.As(err, &existsErr) {
        return b.GetStack(ctx, ref) // adopt existing stack
    }
    return err
}

Prevention

When it happens

Trigger: b.CreateStack returns an error that is neither StackAlreadyExistsError nor OverStackLimitError — e.g. 401 unauthorized, invalid stack name rejected by the backend, or transient API errors.

Common situations: Expired credentials during `pulumi stack init`, stack names violating backend naming rules, hitting API errors on self-hosted backends, or organization-level restrictions other than the stack limit.

Related errors


AI-assisted analysis of pulumi/pulumi@793f7b2e16 (2026-08-31). Data as JSON: /api/errors/1aa5eb98e78b00b5. Report an issue: GitHub.