Skip to content

Commit

Permalink
Handle case when AWS region is configured as blank string (#2206)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolk authored and stocaaro committed Nov 7, 2024
1 parent 83f314c commit 05534b2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/short-dryers-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': patch
---

Handle case when AWS region is configured as blank string
13 changes: 13 additions & 0 deletions packages/cli/src/command_middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/src/command_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.',
});
}
};

/**
Expand Down

0 comments on commit 05534b2

Please sign in to comment.