Skip to content

Instantly share code, notes, and snippets.

@oofnikj
Created July 3, 2020 20:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oofnikj/f45f25c5bd52a182cf480def3f2a95aa to your computer and use it in GitHub Desktop.
Save oofnikj/f45f25c5bd52a182cf480def3f2a95aa to your computer and use it in GitHub Desktop.
awsp: AWS profile switcher
### 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
@peterfication
Copy link

Thanks for this script. Exactly what I needed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment