diff --git a/internal/pkg/cli/env_init.go b/internal/pkg/cli/env_init.go index f317c41cafc..4e7f0b4a4dc 100644 --- a/internal/pkg/cli/env_init.go +++ b/internal/pkg/cli/env_init.go @@ -429,8 +429,15 @@ func (o *initEnvOpts) askEnvSession() error { selCreds, err := o.selCreds() if err != nil { - return err + errRetrieveCreds := err + sess, err := o.sessProvider.Default() + if err != nil { + return errors.Join(errRetrieveCreds, fmt.Errorf("falling back on default credentials: %w", err)) + } + o.sess = sess + return nil } + sess, err := selCreds.Creds(fmt.Sprintf(fmtEnvInitCredsPrompt, color.HighlightUserInput(o.name)), envInitCredsHelpPrompt) if err != nil { return fmt.Errorf("select creds: %w", err) diff --git a/internal/pkg/cli/env_init_test.go b/internal/pkg/cli/env_init_test.go index 34b4812d19e..5aefa86de6f 100644 --- a/internal/pkg/cli/env_init_test.go +++ b/internal/pkg/cli/env_init_test.go @@ -356,7 +356,8 @@ func TestInitEnvOpts_Ask(t *testing.T) { inAdjustVPCVars adjustVPCVars inInternalALBSubnets []string - setupMocks func(mocks initEnvMocks) + getMockCredsSelector func() (credsSelector, error) + setupMocks func(mocks initEnvMocks) wantedError error }{ @@ -416,6 +417,21 @@ func TestInitEnvOpts_Ask(t *testing.T) { m.selCreds.EXPECT().Creds("Which credentials would you like to use to create test?", gomock.Any()).Return(mockSession, nil) }, }, + "should fallback on default session if credentials cant be found": { + inAppName: mockApp, + inEnv: mockEnv, + inDefault: true, + getMockCredsSelector: func() (credsSelector, error) { + return nil, mockErr + }, + setupMocks: func(m initEnvMocks) { + m.sessProvider.EXPECT().Default().Return(&session.Session{ + Config: &aws.Config{ + Region: aws.String("us-west-2"), + }, + }, nil) + }, + }, "should prompt for region if user configuration does not have one": { inAppName: mockApp, inEnv: mockEnv, @@ -923,6 +939,9 @@ func TestInitEnvOpts_Ask(t *testing.T) { sessProvider: mocks.sessProvider, selVPC: mocks.selVPC, selCreds: func() (credsSelector, error) { + if tc.getMockCredsSelector != nil { + return tc.getMockCredsSelector() + } return mocks.selCreds, nil }, ec2Client: mocks.ec2Client,