Skip to content

Commit 05534b2

Browse files
Kamil Sobolstocaaro
authored andcommitted
Handle case when AWS region is configured as blank string (#2206)
1 parent 83f314c commit 05534b2

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

.changeset/short-dryers-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-cli': patch
3+
---
4+
5+
Handle case when AWS region is configured as blank string

packages/cli/src/command_middleware.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ void describe('commandMiddleware', () => {
122122
}
123123
});
124124

125+
void it('throws error if region is blank', async () => {
126+
process.env.AWS_REGION = '';
127+
delete process.env.AWS_DEFAULT_REGION;
128+
try {
129+
await commandMiddleware.ensureAwsCredentialAndRegion(
130+
{} as ArgumentsCamelCase<{ profile: string | undefined }>
131+
);
132+
assert.fail('expect to throw error');
133+
} catch (err) {
134+
assert.match((err as Error).message, /The AWS region is blank/);
135+
}
136+
});
137+
125138
void it('throws error if a profile is provided and no other credential providers', async () => {
126139
try {
127140
await commandMiddleware.ensureAwsCredentialAndRegion({

packages/cli/src/command_middleware.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ export class CommandMiddleware {
6060
}
6161

6262
// Check region.
63+
let region: string | undefined = undefined;
6364
try {
64-
await loadConfig(NODE_REGION_CONFIG_OPTIONS, {
65+
region = await loadConfig(NODE_REGION_CONFIG_OPTIONS, {
6566
ignoreCache: true,
6667
})();
6768
} catch (err) {
@@ -77,6 +78,13 @@ export class CommandMiddleware {
7778
err as Error
7879
);
7980
}
81+
if (!region.trim()) {
82+
throw new AmplifyUserError('InvalidCredentialError', {
83+
message: 'The AWS region is blank',
84+
resolution:
85+
'Ensure that a valid AWS region is provided in profile configuration or AWS_REGION environment variable.',
86+
});
87+
}
8088
};
8189

8290
/**

0 commit comments

Comments
 (0)