diff --git a/.changeset/short-dryers-tell.md b/.changeset/short-dryers-tell.md new file mode 100644 index 0000000000..267a7abdb4 --- /dev/null +++ b/.changeset/short-dryers-tell.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/backend-cli': patch +--- + +Handle case when AWS region is configured as blank string diff --git a/packages/cli/src/command_middleware.test.ts b/packages/cli/src/command_middleware.test.ts index 313b4a2003..386e4c7ab2 100644 --- a/packages/cli/src/command_middleware.test.ts +++ b/packages/cli/src/command_middleware.test.ts @@ -122,6 +122,19 @@ void describe('commandMiddleware', () => { } }); + void it('throws error if region is blank', async () => { + process.env.AWS_REGION = ''; + delete process.env.AWS_DEFAULT_REGION; + try { + await commandMiddleware.ensureAwsCredentialAndRegion( + {} as ArgumentsCamelCase<{ profile: string | undefined }> + ); + assert.fail('expect to throw error'); + } catch (err) { + assert.match((err as Error).message, /The AWS region is blank/); + } + }); + void it('throws error if a profile is provided and no other credential providers', async () => { try { await commandMiddleware.ensureAwsCredentialAndRegion({ diff --git a/packages/cli/src/command_middleware.ts b/packages/cli/src/command_middleware.ts index a06d288f74..c1da30912d 100644 --- a/packages/cli/src/command_middleware.ts +++ b/packages/cli/src/command_middleware.ts @@ -60,8 +60,9 @@ export class CommandMiddleware { } // Check region. + let region: string | undefined = undefined; try { - await loadConfig(NODE_REGION_CONFIG_OPTIONS, { + region = await loadConfig(NODE_REGION_CONFIG_OPTIONS, { ignoreCache: true, })(); } catch (err) { @@ -77,6 +78,13 @@ export class CommandMiddleware { err as Error ); } + if (!region.trim()) { + throw new AmplifyUserError('InvalidCredentialError', { + message: 'The AWS region is blank', + resolution: + 'Ensure that a valid AWS region is provided in profile configuration or AWS_REGION environment variable.', + }); + } }; /**