Created
July 3, 2020 20:15
-
-
Save oofnikj/f45f25c5bd52a182cf480def3f2a95aa to your computer and use it in GitHub Desktop.
awsp: AWS profile switcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### This file is intended to be sourced from ~/.bashrc ### | |
# quickly switch between AWS profiles with auto-completion | |
# uses https://github.com/Nike-Inc/gimme-aws-creds to obtain credentials | |
# if using static credentials, just comment out lines 13-15 | |
awsp() { | |
if [[ -n $1 ]] ; then | |
# validate input | |
grep -q -w "\[profile ${1}\]" ~/.aws/config || { echo "No such profile $1"; return 1; } | |
export AWS_PROFILE=$1 | |
# check if we already have valid creds | |
if ! aws sts get-caller-identity >&- 2>&-; then | |
gimme-aws-creds --profile $1 | |
fi | |
# set the prompt | |
OLD_PS1=${OLD_PS1:-${PS1}} | |
PS1="\e[31;1m${AWS_PROFILE}\e[0m ${OLD_PS1}" | |
else | |
echo 'no AWS account provided' | |
return 2 | |
fi | |
} | |
## basic bash completion, use fzf completion below instead if available | |
# _awsp() { | |
# COMPREPLY=() | |
# local word="${COMP_WORDS[COMP_CWORD]}" | |
# if [ "$COMP_CWORD" -eq 1 ]; then | |
# COMPREPLY=( $(compgen -W "$(aws configure list-profiles)" -- "$word") ) | |
# else | |
# COMPREPLY=() | |
# fi | |
# } | |
# complete -F _awsp awsp | |
_fzf_complete_awsp() { | |
_fzf_complete --multi --reverse -- "$@" < <( | |
grep -E '\[profile .+]' ~/.aws/config \ | |
| sed -E 's/\[profile (.+)\]/\1/g' \ | |
| sort | |
) | |
} | |
complete -F _fzf_complete_awsp awsp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this script. Exactly what I needed :)