I find myself constantly switching between AWS accounts at work.
Since we use SSO, this is a fairly tedious process without some scripted help.
I’ve been working on awsp
, an AWS profile switcher. I’ve only tested it on bash 5.0 on Mac OS, but it should work on zsh
/ Linux too. awsp
will modify your prompt variable to clearly indicate the active profile.
Tab completion for profiles is available for fzf
and plain bash.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
### 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 >/dev/null 2>&1; 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
)
}
_fzf_complete_awsp_notrigger() {
FZF_COMPLETION_TRIGGER='' _fzf_complete_awsp
}
complete -F _fzf_complete_awsp_notrigger awsp
|