├── .awsAliases ├── .bash_profile └── README.md /.awsAliases: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function _awsListAll() { 4 | 5 | credentialFileLocation=${AWS_SHARED_CREDENTIALS_FILE}; 6 | if [ -z $credentialFileLocation ]; then 7 | credentialFileLocation=~/.aws/credentials 8 | fi 9 | 10 | while read line; do 11 | if [[ $line == "["* ]]; then echo "$line"; fi; 12 | done < $credentialFileLocation; 13 | }; 14 | 15 | function _awsSwitchProfile() { 16 | if [ -z $1 ]; then echo "Usage: awsp profilename"; return; fi 17 | 18 | exists="$(aws configure get aws_access_key_id --profile $1)" 19 | if [[ -n $exists ]]; then 20 | export AWS_DEFAULT_PROFILE=$1; 21 | export AWS_PROFILE=$1; 22 | export AWS_REGION=$(aws configure get region --profile $1); 23 | echo "Switched to AWS Profile: $1"; 24 | aws configure list 25 | fi 26 | }; -------------------------------------------------------------------------------- /.bash_profile: -------------------------------------------------------------------------------- 1 | . ~/.awsAliases 2 | alias awsall="_awsListAll" 3 | alias awsp="_awsSwitchProfile" 4 | alias awswho="aws configure list" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS PROFILE 2 | 3 | ## INTRODUCTION 4 | 5 | Managing multiple AWS accounts from `aws-cli` tends to be a little tedious. 6 | When you need to switch between multiple AWS accounts, it seems to be a hassle keeping a 7 | track of the current account, all the available account names. 8 | 9 | Even setting the variable and using it to switch between accounts doesn't seem like 10 | the right deal. 11 | 12 | export AWS_PROFILE=user2 13 | 14 | 15 | Aliases can be used to manage AWS profiles in a better way. 16 | 17 | With aliases we can do the following: 18 | 19 | * List available profiles 20 | * View current profile configuration 21 | * Switch to another profile 22 | 23 | ## Creating Aliases 24 | 25 | First we will create functions that would be invoked by our Alias 26 | 27 | Create a file as below: 28 | 29 | vim ~/.awsAliases 30 | 31 | Next add the below lines in the file: 32 | 33 | #!/bin/bash 34 | function _awsListAll() { 35 | 36 | credentialFileLocation=${AWS_SHARED_CREDENTIALS_FILE}; 37 | if [ -z $credentialFileLocation ]; then 38 | credentialFileLocation=~/.aws/credentials 39 | fi 40 | 41 | while read line; do 42 | if [[ $line == "["* ]]; then echo "$line"; fi; 43 | done < $credentialFileLocation; 44 | }; 45 | 46 | function _awsSwitchProfile() { 47 | if [ -z $1 ]; then echo "Usage: awsp profilename"; return; fi 48 | 49 | exists="$(aws configure get aws_access_key_id --profile $1)" 50 | if [[ -n $exists ]]; then 51 | export AWS_DEFAULT_PROFILE=$1; 52 | export AWS_PROFILE=$1; 53 | export AWS_REGION=$(aws configure get region --profile $1); 54 | echo "Switched to AWS Profile: $1"; 55 | aws configure list 56 | fi 57 | }; 58 | 59 | Now if you are using *bash*, open bash_profile as below: 60 | 61 | vim ~/.bash_profile 62 | 63 | And add the following files: 64 | 65 | . ~/.awsAliases 66 | alias awsall="_awsListAll" 67 | alias awsp="_awsSwitchProfile" 68 | alias awswho="aws configure list" 69 | 70 | If you are using zshrc, then add the lines in *~/.zshrc* 71 | 72 | Now, reload your bash profile or zshrc as below 73 | 74 | source ~/.bash_profile # For zshrc source ~/.zshrc 75 | 76 | ## Running Aliases 77 | 78 | To list all the profiles use: 79 | 80 | awsall 81 | 82 | To switch between profiles use: 83 | 84 | awsp 85 | 86 | To show current profile details: 87 | 88 | awswho 89 | 90 | ## Credits 91 | 92 | Code based on article written by [Carl Nordenflet](https://twitter.com/carl_nordenfelt) --------------------------------------------------------------------------------