@@ -84,7 +84,7 @@ export class SDK {
84
84
} ) ;
85
85
}
86
86
87
- this . defaultAwsAccount = new DefaultAWSAccount ( defaultCredentialProvider , getCLICompatibleDefaultRegion ( this . profile ) ) ;
87
+ this . defaultAwsAccount = new DefaultAWSAccount ( defaultCredentialProvider , getCLICompatibleDefaultRegionGetter ( this . profile ) ) ;
88
88
this . credentialsCache = new CredentialsCache ( this . defaultAwsAccount , defaultCredentialProvider ) ;
89
89
}
90
90
@@ -137,7 +137,7 @@ export class SDK {
137
137
}
138
138
139
139
public async defaultRegion ( ) : Promise < string | undefined > {
140
- return await getCLICompatibleDefaultRegion ( this . profile ) ;
140
+ return await getCLICompatibleDefaultRegionGetter ( this . profile ) ( ) ;
141
141
}
142
142
143
143
public defaultAccount ( ) : Promise < string | undefined > {
@@ -226,7 +226,7 @@ class DefaultAWSAccount {
226
226
227
227
constructor (
228
228
private readonly defaultCredentialsProvider : Promise < AWS . CredentialProviderChain > ,
229
- private readonly region : Promise < string | undefined > ) {
229
+ private readonly region : ( ) => Promise < string | undefined > ) {
230
230
}
231
231
232
232
/**
@@ -258,7 +258,7 @@ class DefaultAWSAccount {
258
258
const accountId = await this . accountCache . fetch ( creds . accessKeyId , async ( ) => {
259
259
// if we don't have one, resolve from STS and store in cache.
260
260
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 ( ) ;
262
262
const aid = result . Account ;
263
263
if ( ! aid ) {
264
264
debug ( 'STS didn\'t return an account ID' ) ;
@@ -333,31 +333,43 @@ async function makeCLICompatibleCredentialProvider(profile: string | undefined,
333
333
* SDK does not allow us to specify a profile at runtime).
334
334
* - AWS_DEFAULT_PROFILE and AWS_DEFAULT_REGION are also used as environment
335
335
* 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).
336
340
*/
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
+ }
348
362
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
+ }
354
367
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
+ }
359
370
360
- return region ;
371
+ return region ;
372
+ } ;
361
373
}
362
374
363
375
/**
0 commit comments