Skip to content

Blog

Easily Switch AWS Profiles with Tab Completion

02.05.2020 | Devops Tutorial | Austin
Easily Switch AWS Profiles with Tab Completion

I am constantly moving between AWS profiles and wrote a bash function to help me hop between what I need.

Hopefully, someone else finds it useful

#!/bin/bash 

_getprofiles() {
    COMPREPLY=($(compgen -W "$(sed -n 's#^\[\(.*\)\]#\1#p' ~/.aws/credentials | sort -u)" -- "${COMP_WORDS[1]}"))
}


aps () {
    if [ -z "$1" ]; then
        echo $AWS_DEFAULT_PROFILE
        return
    fi
    
    export AWS_DEFAULT_PROFILE=$1
}

complete -F _getprofiles aps

Just put that function into your ~/.bash_profile or something that is sourced when a terminal session starts.

This will add the command aps and you can tab complete any of the existing profiles in your aws config.

Share