Using more than one account at AWS while using aws-cli


How to switch between Amazon accounts while using the aws cli

I know I’m late to the game, but lately I’ve run into a situation where I am managing infrastructure for more than one client, and they both use AWS. While browsing, I’ve created different Chrome profiles for Personal, Client 1, and Client 2. I needed something similar for AWS, and fortunately I’m not the only one who ran into this issue.

Amazon’s answer to this issue is simple, it’s called Named Profiles. Essentially, in your ~/.awws/credentials file, you can create sections.

aws_access_key_id = my personal key id
aws_secret_access_key = my personal secret access key

aws_access_key_id = client1 access key
aws_secret_access_key = client1 secret access key

aws_access_key_id = client2 key id
aws_secret_access_key = client2 access key

Now, all I need to do is add the —profile=client1 flag to my aws commands, or set the AWS_PROFILE shell variable.

aws s3 ls --profile=client1
export AWS_PROFILE=client2 
aws s3 ls

I’m also a fan of always knowing what context I’m in, so I’ve modified my ZSH prompt to show what AWS profile I’m using by adding the following line to my ZSH prompt.

First set the variable in .zshrc

export AWS_PROFILE=default
function aws_profile {
        echo $AWS_PROFILE;

RPROMPT='aws:$(aws_profile) $(battery_charge)'

{% include figure image_path=“/assets/images/screenshots/shell-prompt-with-aws-profile-name.png” alt=“modified zsh prompt” caption=“Modified ZSH prompt.” %}