Last updated: Jan 26, 2024
Reading time·2 min

We can set the AWS CLI profile in CDK commands by adding the --profile flag.
There are multiple ways to set the environment (account and region) of a CDK application:
We don't set it at all - then the stack is considered environment agnostic,
which means that the
CDK CLI will take the
account and region from the profile we've
specified, or the default one if we haven't specified any.
We explicitly set the environment (account and region) when instantiating the stack. This is the recommended approach as it prevents inconsistencies with multiple developers having different local configurations for region/ account, i.e.:
const app = new cdk.App(); new MyCdkStack(app, 'my-cdk-stack', { stackName: 'my-cdk-stack', env: { region: 'us-east-1', account: 1234567890, }, });
CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION environment
variables. These are set and made available by the CDK CLI. Their values
resolve, depending on the profile we've specified, or the
default profile if we haven't set any.In order to set the profile for a CDK command we have to use the --profile
flag. Here's an example with the synth command.
npx aws-cdk synth \ --profile my-profile \ my-stack
Or with the deploy command.
npx aws-cdk deploy \ --profile my-profile \ my-stack
You can learn more about the related topics by checking out the following tutorials: