How to use a different Profile in AWS CDK


Borislav Hadzhiev

Sat Apr 24 20212 min read


Photo by Maria Teneva

Updated on Sat Apr 24 2021

Specify the Profile in a CDK command #

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 - 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 configuration 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, }, });
  • We can use the 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, for example with the synth command:

npx cdk synth \ --profile my-profile \ my-stack

Or with the deploy command:

npx cdk deploy \ --profile my-profile \ my-stack

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee