Search code examples
aws-cdk

aws cdk to use non-default profile


I don't want to use my default aws profile and account for cdk development

So I created a new account and a new profile cdkprof using aws configure --profile cdkprof .

I have verified in ~/.aws/credentials and ~/.aws/config files that the new profile created correctly. running export AWS_PROFILE=cdkprof && aws configure list && aws sts get-caller-identity returns me my profile details correctly.

I have also exported

  • CDK_DEFAULT_REGION,
  • CDK_DEFAULT_ACCOUNT,
  • AWS_DEFAULT_REGION,
  • AWS_PROFILE,
  • AWS_SECRET_ACCESS_KEY and
  • AWS_ACCESS_KEY_ID

and these are available as environment variables in bash.

However when I try to run :

$ npx cdk bootstrap --profile cdkprof   

I get the error

Unable to resolve AWS account to use. It must be either configured when you define your CDK or through the environment

How do I use my new profile and account with the cdk commands?

Thanks.


Solution

  • By default, CDK commands will use the default AWS CLI profile. However, you can specify a named profile for a project by adding it to the file which handles CDK commands. For a TypeScript project, this is done in cdk.json at the project root:

    {
      "app": "npx ts-node --prefer-ts-exts bin/project.ts",
      "profile": "cdkprof",
      "context": {
        ...
      }
    }
    

    Then, your named profile will be used when you run commands such as cdk bootstrap.