If you're switching back and forth a lot between profiles, you may want some indicator of which profile you're currently on. This can avoid unnecessary permissions errors or worse!
There are many ways to solve this problem, but one common solution is to add the current aws profile to your shell prompt. And again, there are a few ways to do this.
zsh (using oh-my-zsh)
Since zsh
is the default shell on macOS now, we'll start here. Oh My Zsh is a popular framework for managing zsh configs.
If you're using zsh (or would like to), you can simply add the following in your .zshrc
:
plugins=(... aws)
This will automatically install the aws plugin when you open a new shell. It gives you access to a few new commands and adds current aws profile to your prompt by default. Check out the docs for more configuration options.
bash or zshrc
If you search google, there are plenty of scripts people have written to simplify this process. But I'll add a super simple method here. In your ~/.bashrc
or ~/.zshrc
file, add the following function:
function aws_profile {
_profile=$(aws configure list | egrep profile | awk '{print "("$2")"}')
if [[ "${_profile}" == "(<not)" ]]; then
echo "(none)"
else
echo "${_profile}"
fi
}
Then modify your prompt using the PROMPT
variable in zsh or the PS1
variable in bash. The following is an example using zsh.
PROMPT='$(aws_profile)'$PROMPT$'\n$ '