Skip to content

Commit 0c72ef3

Browse files
authoredJun 3, 2019
fix(cli): don't fail if region cannot be determined (#2721)
No longer fail if an AWS Region cannot be determined anymore from current AWS config. Fixes #2697.
1 parent 2754dde commit 0c72ef3

File tree

1 file changed

+37
-25
lines changed
  • packages/aws-cdk/lib/api/util

1 file changed

+37
-25
lines changed
 

‎packages/aws-cdk/lib/api/util/sdk.ts

+37-25
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class SDK {
8484
});
8585
}
8686

87-
this.defaultAwsAccount = new DefaultAWSAccount(defaultCredentialProvider, getCLICompatibleDefaultRegion(this.profile));
87+
this.defaultAwsAccount = new DefaultAWSAccount(defaultCredentialProvider, getCLICompatibleDefaultRegionGetter(this.profile));
8888
this.credentialsCache = new CredentialsCache(this.defaultAwsAccount, defaultCredentialProvider);
8989
}
9090

@@ -137,7 +137,7 @@ export class SDK {
137137
}
138138

139139
public async defaultRegion(): Promise<string | undefined> {
140-
return await getCLICompatibleDefaultRegion(this.profile);
140+
return await getCLICompatibleDefaultRegionGetter(this.profile)();
141141
}
142142

143143
public defaultAccount(): Promise<string | undefined> {
@@ -226,7 +226,7 @@ class DefaultAWSAccount {
226226

227227
constructor(
228228
private readonly defaultCredentialsProvider: Promise<AWS.CredentialProviderChain>,
229-
private readonly region: Promise<string | undefined>) {
229+
private readonly region: () => Promise<string | undefined>) {
230230
}
231231

232232
/**
@@ -258,7 +258,7 @@ class DefaultAWSAccount {
258258
const accountId = await this.accountCache.fetch(creds.accessKeyId, async () => {
259259
// if we don't have one, resolve from STS and store in cache.
260260
debug('Looking up default account ID from STS');
261-
const result = await new AWS.STS({ credentials: creds, region: await this.region }).getCallerIdentity().promise();
261+
const result = await new AWS.STS({ credentials: creds, region: await this.region() }).getCallerIdentity().promise();
262262
const aid = result.Account;
263263
if (!aid) {
264264
debug('STS didn\'t return an account ID');
@@ -333,31 +333,43 @@ async function makeCLICompatibleCredentialProvider(profile: string | undefined,
333333
* SDK does not allow us to specify a profile at runtime).
334334
* - AWS_DEFAULT_PROFILE and AWS_DEFAULT_REGION are also used as environment
335335
* variables to be used to determine the region.
336+
*
337+
* Returns a function that can be invoked to retrieve the actual region value
338+
* (used to be just a promise, but that would lead to firing off a failing
339+
* operation and if it was never awaited NodeJS would complain).
336340
*/
337-
async function getCLICompatibleDefaultRegion(profile: string | undefined): Promise<string | undefined> {
338-
profile = profile || process.env.AWS_PROFILE || process.env.AWS_DEFAULT_PROFILE || 'default';
339-
340-
// Defaults inside constructor
341-
const toCheck = [
342-
{filename: process.env.AWS_SHARED_CREDENTIALS_FILE },
343-
{isConfig: true, filename: process.env.AWS_CONFIG_FILE},
344-
];
345-
346-
let region = process.env.AWS_REGION || process.env.AMAZON_REGION ||
347-
process.env.AWS_DEFAULT_REGION || process.env.AMAZON_DEFAULT_REGION;
341+
function getCLICompatibleDefaultRegionGetter(profile: string | undefined): () => Promise<string | undefined> {
342+
let retrieved = false;
343+
let region: string | undefined;
344+
return async () => {
345+
if (!retrieved) {
346+
profile = profile || process.env.AWS_PROFILE || process.env.AWS_DEFAULT_PROFILE || 'default';
347+
348+
// Defaults inside constructor
349+
const toCheck = [
350+
{filename: process.env.AWS_SHARED_CREDENTIALS_FILE },
351+
{isConfig: true, filename: process.env.AWS_CONFIG_FILE},
352+
];
353+
354+
region = process.env.AWS_REGION || process.env.AMAZON_REGION ||
355+
process.env.AWS_DEFAULT_REGION || process.env.AMAZON_DEFAULT_REGION;
356+
357+
while (!region && toCheck.length > 0) {
358+
const configFile = new SharedIniFile(toCheck.shift());
359+
const section = await configFile.getProfile(profile);
360+
region = section && section.region;
361+
}
348362

349-
while (!region && toCheck.length > 0) {
350-
const configFile = new SharedIniFile(toCheck.shift());
351-
const section = await configFile.getProfile(profile);
352-
region = section && section.region;
353-
}
363+
if (!region) {
364+
const usedProfile = !profile ? '' : ` (profile: "${profile}")`;
365+
debug(`Unable to determine AWS region from environment or AWS configuration${usedProfile}`);
366+
}
354367

355-
if (!region) {
356-
const usedProfile = !profile ? '' : ` (profile: "${profile}")`;
357-
throw new Error(`Unable to determine AWS region from environment or AWS configuration${usedProfile}`);
358-
}
368+
retrieved = true;
369+
}
359370

360-
return region;
371+
return region;
372+
};
361373
}
362374

363375
/**

0 commit comments

Comments
 (0)