As I work with multiple AWS accounts every day I need to switch between them very often. It’s a key issue for me to always be sure on which account I work and execute AWS CLI command.
So I wrote a quick bash function that I put in my .bashrc. The function is used in my PS1 prompt and shows current AWS CLI profile (from env variable AWS_PROFILE):
aws_profile() { if [[ -n $AWS_PROFILE ]]; then echo -n "[$AWS_PROFILE] " else echo -n "" fi } PS1="\$(aws_profile)\[\033[01;34m\]\w\[\033[00m\]\$ "
I also have two handy aliases for easy and reliable setting of AWS_PROFILE env variable, without the need to call export AWS_PROFILE=profile_name each time:
setprofile alias checks if the provided profile name is present in ~/.aws/config so you don’t have to worry about any typos :)
clearprofile alias – as the name suggests – clears profile name in AWS_PROFILE env variable, so you now use default profile.
The code for these aliases:
alias clearprofile='export AWS_PROFILE=' setprofile() { if [ $(cat ~/.aws/config | grep "\[profile $1\]" | wc -l) == 0 ]; then echo "Invalid profile name" else export AWS_PROFILE=$1 fi }
I hope these ticks will make your work easier :)
Leave a Comment