How to use a different Profile in AWS CDK

avatar
Borislav Hadzhiev

Last updated: Jan 26, 2024
2 min

banner

# 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 - 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, }, });
  • 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. Here's an example with the synth command.

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

Or with the deploy command.

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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.