cli: support AWS_PROFILE environment variable for using named profiles
- [x] :beetle: bug report
-
What is the current behavior? CDK doesn't use exported AWS_PROFILE
cdk deploy Unable to resolve AWS account to use. It must be either configured when you define your CDK or through the environment -
What is the expected behavior (or behavior of feature suggested)?
export AWS_PROFILE=test
cdk deploy should read profile from AWS_PROFILE and deploy the template
-
What is the motivation / use case for changing the behavior or adding this feature? It should work using same env variables as AWS CLI
-
Please tell us about your environment:
- CDK CLI Version1.1.0 (build 1a11e96)
- Module Version: xx.xx.xx
- OS: Manjaro Linux
- Language: all
Is there any workaround or info on this, I am hitting this same issue and not sure how to really get around this since I use multiple AWS profiles and such and even after exporting and supplying it getting the error.
@laur1s @btotharye - have you been able to work around this issue?
You're right that the environment AWS_PROFILE is not read or used. Assuming you created a new profile using something like aws configure --profile myProfile, you would run your cdk commands with the profile argument
i.e. cdk deploy --profile myProfile
I'd also suggest reading up on the Specifying your credentials and region section in the documentation.
Let me know if that works for you or if there are still gaps in the documentation that we can improve!
I'm also wondering if the CLI should also support the AWS_PROFILE environment variable and that exporting a named profile is more intuitive for AWS CLI users who are using the CDK CLI.
actually, I'm able to switch between profiles and synth/deploy without issues
repro steps:
- configure a profile with
aws configure --profile myProfile export AWS_PROFILE=myProfilecdk deployexport AWS_PROFILE=default(same account, different region)cdk deploy
I can see that the stacks are being respected with deploys. does cdk doctor list the correct profile? And also, does aws configure list seem properly populated when switching profiles?
@laur1s @btotharye - have you been able to work around this issue?
You're right that the environment
AWS_PROFILEis not read or used. Assuming you created a new profile using something likeaws configure --profile myProfile, you would run your cdk commands with the profile argumenti.e.
cdk deploy --profile myProfileI'd also suggest reading up on the
Specifying your credentials and regionsection in the documentation.Let me know if that works for you or if there are still gaps in the documentation that we can improve!
I'm also wondering if the CLI should also support the
AWS_PROFILEenvironment variable and that exporting a named profile is more intuitive for AWS CLI users who are using the CDK CLI.
I use a python script to give it my MFA token and then that exports a AWS_PROFILE for me that I use to get around it.
@btotharye gotcha, is the MFA the primary problem you're running into? have you seen/tried the approach mentioned in #1246
No activity, so closing issue for now
Feel free to reopen if the status changes!
Not sure if this help, but in my organisation we use AWS SSO, with over 90 AWS Accounts, with MFA, so it was impossible to manage CDK without AWS CLIv2 with profiles, and AWS SSO support.
I found a way to solve it, so till its officially released, you can use this, it works quite all right: MatsCloud blog - CDK with AWS SSO multi account multi profile
To solve this, I added this following alias to my zshrc file: alias cdk='cdk --profile $AWS_PROFILE'
Not worth reopening I guess, but to help any future travellers that wind up here:
I recently encountered this on a fresh install using credential helpers: CDK did not respect $AWS_PROFILE even when it was definitely available in the environment (env), exported, and even visible to node (node, process.env.AWS_PROFILE).
...But the problem magically fixed after I created a blank credentials file: touch ~/.aws/credentials. Now cdk is automatically using my selected AWS_PROFILE.
So if you're in a context where you don't have a credentials file created because you don't really use one - maybe give this a try!
Maybe it will save to somebody a time. I had on a build agent only one ~/.aws/config file
[default]
output = json
region = eu-west-1
[profile develop]
region = eu-west-1
role_arn = arn:aws:iam::<AWS_ACCOUNT_ID>:role/<ASSUME_ROLE_CROSS_ACCOUNT>
credential_source = Environment
[profile production]
region = eu-west-1
role_arn = arn:aws:iam::<AWS_ACCOUNT_ID>:role/<ASSUME_ROLE_CROSS_ACCOUNT>
credential_source = Environment
and it was absolutely enough to work with aws --profile "<profile name>" ... commands with a passed AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables to a build.
But npx cdk deploy "<stack-name>" --profile "<profile name>" command was failed by error: Unable to resolve AWS account to use. It must be either configured when you define your CDK Stack, or through the environment
And solution from athewsey was really helpful for my case too.
+1 on this. I'd love it if aws-cdk-cli supports AWS_PROFILE natively so that you can avoid using the --profile flag every time. In the meantime, thanks for the ~/.zshrc workaround!
# CDK with Profile
alias cdk='cdk profile="${AWS_PROFILE}"'