This blog post shows you a simple way to switch between different AWS profiles on the command line.
- aws cli
- You are using zsh as your shell.
If you have worked with multiple AWS accounts and use the
file to store your profiles, you will have a file full of the following statements:
To set an AWS profile on the command line via the environment variable, you do the following:
or if you want to set the access key and secret key
The problem with the above approach is:
You have to manually look up the
~/.aws/credentialsfile and copy paste the right values.
When the no. of AWS accounts increase, the profiles associated with them increase and you have to grep for the profile name in the file.
You should have a simple way to set the AWS_PROFILE on the command line with some sort of autocompletion. Without doing too much work.
zsh has a very cool feature called zsh completions which can help us in this case.
Carry out the following steps to enable zsh commands which help you list and set your AWS profiles and keys with autocompletion:
- Create the completions directory:
# mkdir -p ~/.oh-my-zsh/completions
- Change dir to the completions directory:
# cd ~/.oh-my-zsh/completions </pre>
~/.oh-my-zsh/completions/_set-aws-profilefile (without the .sh extension):
~/.oh-my-zsh/completions/_set-aws-keysfile (without the .sh extension):
Add the supporting commands to your
- Open a new terminal and source your new config:
# source ~/.zshrc
- Give it a spin: