DEV Community

loading...
Focused Labs

Easily Switch AWS Profiles with Tab Completion

Austin Vance
Originally published at focusedlabs.io Updated on ・1 min read

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

Enter fullscreen mode Exit fullscreen mode

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.

Discussion (0)