{"record":{"id":"1aa5eb98e78b00b5","repo":"pulumi/pulumi","slug":"could-not-create-stack-w","errorCode":null,"errorMessage":"could not create stack: %w","messagePattern":"could not create stack: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/cmd/pulumi/stack/io.go","lineNumber":438,"sourceCode":"\t\tif !found {\n\t\t\treturn nil, errors.New(\"could not get project from stack reference\")\n\t\t}\n\t\tescEnvironment = proj.String() + \"/\" + stackRef.Name().String()\n\t\tbackendOpts.Config = &apitype.StackConfig{\n\t\t\tEnvironment: escEnvironment,\n\t\t}\n\t}\n\n\tstack, err := b.CreateStack(ctx, stackRef, root, initialState, &backendOpts)\n\tif err != nil {\n\t\t// If it's a well-known error, don't wrap it.\n\t\tif _, ok := err.(*backenderr.StackAlreadyExistsError); ok {\n\t\t\treturn nil, err\n\t\t}\n\t\tif _, ok := err.(*backenderr.OverStackLimitError); ok {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn nil, fmt.Errorf(\"could not create stack: %w\", err)\n\t}\n\n\tif !opts.Quiet {\n\t\tsink.Infof(diag.Message(\"\", \"Created stack '%s'\"), stack.Ref())\n\t}\n\n\tif escEnvironment != \"\" {\n\t\t// Shared helper without a *cobra.Command writer; uses process stdout.\n\t\tfmt.Printf(\"Created environment %s for stack configuration\\n\", escEnvironment) //nolint:forbidigo\n\t}\n\n\t// Now that we've created the stack, we'll write out any necessary configuration changes.\n\tif needsSave {\n\t\terr = SaveProjectStack(ctx, stack, ps, opts.ConfigFile)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"%w: %w\", ErrSaveStackConfig, err)\n\t\t}\n\t}","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/pulumi/pulumi/blob/793f7b2e160db4321fb7fb6b0607461e01cb251e/pkg/cmd/pulumi/stack/io.go#L420-L456","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the wrapped cause (%w) to identify the backend error and fix it (login, naming, permissions)","Check whether the failure is actually duplicate-exists or over-limit, which surface as their own typed errors","Re-authenticate with `pulumi login` / valid PULUMI_ACCESS_TOKEN","Retry if the wrapped error indicates a transient 5xx/network condition"],"exampleFix":"// before\n$ pulumi stack init prod\nerror: could not create stack: 401 unauthorized\n// after\n$ pulumi login\n$ pulumi stack init prod","handlingStrategy":"type-guard","validationCode":"// pre-validate name and auth\nif err := exec.Command(\"pulumi\", \"whoami\").Run(); err != nil {\n    return errors.New(\"login required before stack init\")\n}","typeGuard":"var existsErr *backenderr.StackAlreadyExistsError\nvar limitErr *backenderr.OverStackLimitError\nif errors.As(err, &existsErr) { /* already exists */ }\nelse if errors.As(err, &limitErr) { /* over limit */ }\nelse { /* wrapped: could not create stack: %v */ }","tryCatchPattern":"var existsErr *backenderr.StackAlreadyExistsError\nif err != nil {\n    if errors.As(err, &existsErr) {\n        return b.GetStack(ctx, ref) // adopt existing stack\n    }\n    return err\n}","preventionTips":["Refresh credentials before scripted stack init","Check `pulumi stack ls` for existing names to avoid duplicates","Validate stack names against backend naming rules","For OverStackLimitError, use an org with higher limits or delete unused stacks"],"tags":["stack-creation","backend","cli"],"backgroundTag":"stack-creation-failed","analyzedSha":"793f7b2e160db4321fb7fb6b0607461e01cb251e","analyzedAt":"2026-08-31T09:36:43.099Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}