Script Day: AWS CLI with multiple accounts with ease

Maybe you are a consultant and juggle multiple clients with Amazon Web Services deployments, maybe you just have accounts for all the start-ups you ever worked for, or maybe you just like to use 17 different AWS accounts for the free-tier usage, but eventually your ~/.aws/credentials file looks like an MS-Windows INI file.

At this point, running the AWS CLI is kind of annoying – you need to remember the correct --profile flag to set for each scenario, and bash will not complete these for you…

Bash aliases to the rescue!

In your ~/.bash_aliases (or your ~/.bashrc if you have no such file, some operating systems like to deploy it, but most don’t), add the following code:

 

# set up AWS aliases
for profile in $(perl -nle 'm/\[([^]]+)\]/ and $1 ne "default" && print $1' < ~/.aws/credentials); do
    alias aws-$profile="aws --profile $profile"
done

 

And now all your AWS profiles are accessible by running aws-<profile>, and bash will love to auto-complete those.

For example, to access my geek profile, I run aws-geek instead of aws --profile geek. The default profile still gets invoked when you simply run aws, so no change there.

Leave a Reply