@@ -14,6 +14,7 @@ import { Worker } from 'node:worker_threads';
1414import { getModuleDirname , getProjectDirname } from '../getDirname.js' ;
1515import { findNpmPath } from '../utils/findNpmPath.js' ;
1616import { type BundlingOptions } from 'aws-cdk-lib/aws-lambda-nodejs' ;
17+ import { loadSharedConfigFiles } from '@smithy/shared-ini-file-loader' ;
1718
1819/**
1920 * Support for AWS CDK framework
@@ -429,6 +430,8 @@ export class CdkFramework implements IFramework {
429430 'aws:cdk:bundling-stacks' : [ ] ,
430431 } ;
431432 process . env . CDK_CONTEXT_JSON = JSON . stringify ( CDK_CONTEXT_JSON ) ;
433+ process . env . CDK_DEFAULT_REGION =
434+ config . region ?? ( await this . getRegion ( config . profile ) ) ;
432435 Logger . verbose ( `[CDK] Context:` , JSON . stringify ( CDK_CONTEXT_JSON , null , 2 ) ) ;
433436
434437 const awsCdkLibPath = await findNpmPath (
@@ -647,6 +650,70 @@ export class CdkFramework implements IFramework {
647650
648651 return entryFile ;
649652 }
653+
654+ /**
655+ * Attempts to get the region from a number of sources and falls back to us-east-1 if no region can be found,
656+ * as is done in the AWS CLI.
657+ *
658+ * The order of priority is the following:
659+ *
660+ * 1. Environment variables specifying region, with both an AWS prefix and AMAZON prefix
661+ * to maintain backwards compatibility, and without `DEFAULT` in the name because
662+ * Lambda and CodeBuild set the $AWS_REGION variable.
663+ * 2. Regions listed in the Shared Ini Files - First checking for the profile provided
664+ * and then checking for the default profile.
665+ * 3. xxx
666+ * 4. us-east-1
667+ *
668+ * Code from aws-cdk-cli/packages/@aws-cdk/tmp-toolkit-helpers/src/api/aws-auth
669+ /awscli-compatible.ts
670+ */
671+ protected async getRegion (
672+ maybeProfile ?: string ,
673+ ) : Promise < string | undefined > {
674+ const profile =
675+ maybeProfile ||
676+ process . env . AWS_PROFILE ||
677+ process . env . AWS_DEFAULT_PROFILE ||
678+ 'default' ;
679+
680+ const region =
681+ process . env . AWS_REGION ||
682+ process . env . AMAZON_REGION ||
683+ process . env . AWS_DEFAULT_REGION ||
684+ process . env . AMAZON_DEFAULT_REGION ||
685+ ( await this . getRegionFromIni ( profile ) ) ;
686+
687+ return region ;
688+ }
689+
690+ /**
691+ * Looks up the region of the provided profile. If no region is present,
692+ * it will attempt to lookup the default region.
693+ * @param profile The profile to use to lookup the region
694+ * @returns The region for the profile or default profile, if present. Otherwise returns undefined.
695+ *
696+ * Code from aws-cdk-cli/packages/@aws-cdk/tmp-toolkit-helpers/src/api/aws-auth
697+ */
698+ private async getRegionFromIni ( profile : string ) : Promise < string | undefined > {
699+ const sharedFiles = await loadSharedConfigFiles ( { ignoreCache : true } ) ;
700+ return (
701+ this . getRegionFromIniFile ( profile , sharedFiles . credentialsFile ) ??
702+ this . getRegionFromIniFile ( profile , sharedFiles . configFile ) ??
703+ this . getRegionFromIniFile ( 'default' , sharedFiles . credentialsFile ) ??
704+ this . getRegionFromIniFile ( 'default' , sharedFiles . configFile )
705+ ) ;
706+ }
707+
708+ /**
709+ * Get region from ini file
710+ * @param profile
711+ * @param data
712+ * @returns
713+ */
714+ private getRegionFromIniFile ( profile : string , data ?: any ) {
715+ return data ?. [ profile ] ?. region ;
716+ }
650717}
651718
652719export const cdkFramework = new CdkFramework ( ) ;
0 commit comments