Python Script to Change AWS Profiles

Reading time ~2 minutes

Want to learn more about AWS, Scripts and Python? Sign up for my mailing list!

I frequently find myself switching between AWS profiles, especially when I’m home from work switching between side projects on different AWS accounts. I got tired of opening and editing my credentials file manually so I wrote a Python script and a Bash function to do it for me. You can use it too!

My AWS profiles in the ~/.aws/credentials file look a little like this:

aws_access_key_id = EXAMPLEASDHASKDASD
aws_secret_access_key = ASDJEXAMPLEYsbclahsd/aDGSHJASD
aws_access_key_id = ANOTHEREXAMPLEASIDGUYasdgasd
aws_secret_access_key = LASTEXAMPLETHING/asidughhasd
aws_access_key_id = HOWMANYPROFILESDOIHAVEfernando
aws_secret_access_key = tooManyPROfiLESSRSLytoomany/21t76eygudasd
aws_access_key_id = HOWMANYPROFILESDOIHAVEfernando
aws_secret_access_key = tooManyPROfiLESSRSLytoomany/21t76eygudasd


  • It’s a really lazy script, and it expects your credentials file to be formatted identically to mine.
  • Back up your credentials file if you’re worried (but this script does it for you in a backup_credentials file right next to the file)

Here’s the code to put in your .bash_profile or .zsh_profile or .whatever_profile to call the script:

    cd ~/.aws
    echo swapping to $1
    python $1
    cd -

awsps is short for AWS profile swap. After saving the file, be sure to source .your_profile where .your_profile is the one you edited.

Now you can get the script. The most recent version of the script can be found on my github. It’s pretty well commented so take a look over everything.

Open up your terminal and you can grab it with:


Once you’re done, copy the script to the same directory as your AWS credentials file. This will (probably) do it for you.

cp ~/.aws/

The basic usage once you’ve done all the setup would be awsps fernando, where fernando is the name of the profile you’d like to switch to. If this works you should see something like this:

swapping to fernando
Success - Swapped profile to fernando

You can also restore your profile from the backup with awsps restore.

Questions? Let me know on Twitter.

Ideas for code changes? Open a Pull Request