Skip to content

Commit cb9fc01

Browse files
fix: Set CDK_DEFAULT_REGION environment variable
1 parent c02e395 commit cb9fc01

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

package-lock.json

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"@aws-sdk/client-lambda": "^3.798.0",
111111
"@aws-sdk/client-s3": "^3.798.0",
112112
"@aws-sdk/credential-providers": "^3.798.0",
113+
"@smithy/shared-ini-file-loade": "^4.0.2",
113114
"aws-iot-device-sdk": "^2.2.15",
114115
"chalk": "^5.4.1",
115116
"chokidar": "^3.6.0",
@@ -131,6 +132,7 @@
131132
"@aws-sdk/client-lambda",
132133
"@aws-sdk/client-s3",
133134
"@aws-sdk/credential-providers",
135+
"@smithy/shared-ini-file-loade",
134136
"aws-iot-device-sdk",
135137
"chokidar",
136138
"commander",

src/frameworks/cdkFramework.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Worker } from 'node:worker_threads';
1414
import { getModuleDirname, getProjectDirname } from '../getDirname.js';
1515
import { findNpmPath } from '../utils/findNpmPath.js';
1616
import { 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

652719
export const cdkFramework = new CdkFramework();

test/cdk-basic/bin/cdk-basic.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import * as cdk from 'aws-cdk-lib';
44
import { CdkbasicStack } from '../lib/cdk-basic-stack';
55
import { CdkbasicStack2 } from '../lib/subfolder/cdk-basic-stack2';
66

7+
if (process.env.CDK_DEFAULT_REGION !== 'eu-west-1') {
8+
// checking if the region is set with Lambda Live Debugger
9+
throw new Error('CDK_DEFAULT_REGION must be set to eu-west-1');
10+
}
11+
712
const app = new cdk.App();
813

914
const environment = app.node.tryGetContext('environment');

0 commit comments

Comments
 (0)