This blog post shows you a simple way to switch between different AWS profiles on the command line.
Pre-requisites:
Scenario:
If you have worked with multiple AWS accounts and use the ~/.aws/credentials
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
Problem:
The problem with the above approach is:
-
You have to manually look up the
~/.aws/credentials
file 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.
Requirement:
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.
Solution:
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>
-
Create
~/.oh-my-zsh/completions/_set-aws-profile
file (without the .sh extension): -
Create
~/.oh-my-zsh/completions/_set-aws-keys
file (without the .sh extension): -
Add the supporting commands to your
~/.zshrc
file: - Open a new terminal and source your new config:
# source ~/.zshrc
- Give it a spin: