AWS-CLI Profile Management Made Easy

AWS-CLI profile management on your local desktop can be a hassle. Especially if you frequently switch between different accounts. Keeping track of available profiles, switching between them and knowing which one you’re using at the time is tedious. Setting up named profiles helps a little bit but it doesn’t take you all the way. I personally don’t like the –profile argument, I’m too lazy to use that.

I set out to come to terms with this and came up with three aliases that I can use in my shell. They allow me to list available profiles, view the current profile configuration and switch to another profile.

First of all, you have to configure the aws-cli with named profiles. If you haven’t done so, the aws-cli documentation will guide you through this in a few easy steps.
Once you have your profiles configured we can move on to the next step; setting up the aliases.

Create the aliases

This code snippet contains a couple of functions that we will invoke with the new aliases.
Create a new file and paste the code snippet below, hit Ctrl+C to exit.

cat > ~/._awsAliases


function _awsListAll() {

    if [ -z $credentialFileLocation ]; then

    while read line; do
        if [[ $line == "["* ]]; then echo "$line"; fi;
    done < $credentialFileLocation;

function _awsSwitchProfile() {
   if [ -z $1 ]; then  echo "Usage: awsp profilename"; return; fi
   exists="$(aws configure get aws_access_key_id --profile $1)"
   if [[ -n $exists ]]; then
       export AWS_DEFAULT_PROFILE=$1;
       export AWS_PROFILE=$1;
       export AWS_REGION=$(aws configure get region --profile $1);
       echo "Switched to AWS Profile: $1";
       aws configure list

Now open ~/.bash_profile in your favourite editor and add the following lines:

. ~/._awsAliases
alias awsall="_awsListAll"
alias awsp="_awsSwitchProfile"
alias awswho="aws configure list"

List available profiles

To see a list of all available profiles, type:


Switch profile

To switch to another profile, type:

awsp profilename

Show the current profile

The aws-cli actually provides a command for listing the current profile configuration (aws configure list). I find it a bit long to type so I added an alias for this too:


And that’s it. I hope this will help you manage your aws-cli profiles as much as it has helped me. Happy clouding!

PS. Don’t use Access Keys in your production account. That’s bad form ;)

This Post Has 7 Comments

  1. Carl Nordenfelt

    It turns out that, at least the Javascript SDK, does not adhere to AWS_DEFAULT_PROFILE but expects an environment variable named AWS_PROFILE.
    The script above has been updated and now sets both these variables.

  2. Carl Nordenfelt

    Yet another addendum:

    The SDKs (or at least the Javascript SDK) does not pick up the region from the profile configuration.The script above has been updated and now sets the AWS_REGION environment variable to the same region as your profile points to.

  3. Andrei Neculau

    credentialFileLocation=$(env | grep AWS_SHARED_CREDENTIALS_FILE | cut -d= -f2);
    as a “shortcut” for

    ? :)

  4. Corey Quinn

    Any chance you can put this up on GitHub or similar so folks can PR against it?

  5. abril joseph

    Ive been looking for a simple widget to let me quickly configure my AWS CLI profile. No need to look any further.

  6. Pavel

    Great! Thank you for the post. It does work perfectly if you have all credentials (aka aws_access_key_id, aws_secret_access_key) in the .aws/config. Quite often the organisations use the roles and also common (source) profile. For example:
    [profile profile-prod]
    [profile profile-non-prod]

    And the .aws/credentials:

    In this case ._awsAliases -> _awsSwitchProfile() will not be able to change the profile.
    The reason: aws configure get aws_access_key_id –profile $1 will fail to fetch the aws_access_key_id from the .aws/config because they are located in the shared .aws/credentials

    Another thing. If your organisation uses roles then your assumeRole will take precedence over credentials.
    We could update this function a little bit to handle that:
    function _awsSwitchProfile() {

    exists=”$(aws configure get aws_access_key_id –profile $1) || $(aws configure get role_arn –profile $1) || $(aws configure get source_profile –profile $1)”


Leave a Reply