How to use a different Profile in AWS CDK

avatar

Borislav Hadzhiev

Wed Apr 13 20222 min read

banner

Photo by Maria Teneva

Updated - Wed Apr 13 2022

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 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. 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

Further Reading #

Use the search field on my Home Page to filter through my more than 1,000 articles.