├── LICENSE ├── README.md ├── aws-profile ├── aws-wrapper └── install.sh /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Nikolai Zujev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | aws-profile 2 | ========== 3 | 4 | *Make AWS CLI profile switch easy, while working with many accounts.* 5 | 6 | --- 7 | 8 | **NB!** *aws-profile* works **only** with Amazon AWSCLI (Python) package. 9 | 10 | Java-based version is not supported (for now). 11 | 12 | For more details, please refer to [aws.amazon.com/cli](http://aws.amazon.com/cli/) 13 | 14 | *Note: script was tested only in Bash* 15 | 16 | Installation 17 | --- 18 | 19 | Install AWS CLI Tools: 20 | 21 | $ pip install awscli 22 | 23 | Install aws-profile: 24 | 25 | $ curl -sSL https://raw.github.com/jaymecd/aws-profile/master/install.sh | sh 26 | 27 | Or via clone: 28 | 29 | $ git clone https://github.com/jaymecd/aws-profile.git 30 | $ cp aws-profile/aws-* /usr/local/bin 31 | 32 | Update resource file 33 | --- 34 | 35 | Add following lines to `~/.profile` or `~/.bashrc` file: 36 | 37 | export PATH="/usr/local/bin:$PATH" 38 | 39 | # Amazon AWS Service CLI 40 | complete -C aws_completer aws 41 | alias aws-profile="source aws-profile" 42 | alias aws="aws-wrapper" 43 | 44 | *Note: alias for **aws-profile** is required for transparent work, otherwise its requires to call via source manually, cause there is now way to expose environment variables back to parent.* 45 | 46 | Setup config 47 | --- 48 | Populate config `~/.aws/config` with desired profiles (refer AWS manual): 49 | 50 | [profile secret-project] 51 | aws_access_key_id=SOME-ACCESS-KEY-1 52 | aws_secret_access_key=SOME-SECRET-KEY-1 53 | region=us-east-2 54 | 55 | [profile test-user] 56 | aws_access_key_id=SOME-ACCESS-KEY-2 57 | aws_secret_access_key=SOME-SECRET-KEY-2 58 | region=eu-west-1 59 | 60 | Overview profile 61 | --- 62 | 63 | To list profiles, run command without arguments: 64 | 65 | $ aws-profile 66 | 67 | Output explained: 68 | 69 | AWS_DEFAULT_PROFILE : default <-- Current profile 70 | AWS_CONFIG_FILE : /Users/nick/.aws/config <-- Config file 71 | AWS_DEFAULT_REGION : ~ <-- Default region, if set 72 | AWS_DEFAULT_OUTPUT : ~ <-- Default output, if set 73 | 74 | Configured profiles : secret-project test-user <-- Allowed profiles 75 | 76 | Switch between profiles 77 | --- 78 | 79 | To switch aws profile to *secret-project*, run command: 80 | 81 | $ aws-profile secret-project 82 | 83 | To reset back to default, run command: 84 | 85 | $ aws-profile + 86 | 87 | To switch aws profiles off, run command: 88 | 89 | $ aws-profile - 90 | 91 | Run AWS commands: 92 | --- 93 | 94 | Since there is an alias for `aws` original call, nothing is changed, call it as usual: 95 | 96 | $ aws ec2 describe-instances --output text 97 | 98 | Which gives output like: 99 | 100 | NB! Running AWS CLI Tools with secret-project profile. 101 | 102 | 274082975067 r-8bebfdc4 226008221399 103 | … SOME OTHER DATA … 104 | 105 | P.S. console autocomplete still works like a charm. Try `aws TABTAB` 106 | 107 | Bash PS1 prompt hint: 108 | --- 109 | 110 | Update `.profile` or `.bashrc` with following code: 111 | 112 | function __ps_aws() { 113 | [ $AWS_CONFIG_FILE ] && echo " (aws ${AWS_DEFAULT_PROFILE:-default})" 114 | } 115 | 116 | export PS1="\u@\h:\w\$(__ps_aws) \$ " 117 | 118 | And it will looks something like that: 119 | 120 | nick@domain:~ (aws secret-project) $ _ 121 | 122 | --- 123 | 124 | FIN 125 | 126 | P.P.S Patches are welcome 127 | -------------------------------------------------------------------------------- /aws-profile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # This file is part of the aws-profile package. 4 | # 5 | # (c) Nikolai Zujev 6 | # 7 | # For the full copyright and license information, please view the LICENSE 8 | # file that was distributed with this source code. 9 | # 10 | 11 | r="\033[0;31m" # red 12 | w="\033[0;37m" # white 13 | g="\033[0;32m" # green 14 | y="\033[1;33m" # yellow 15 | x="\033[0m" # reset 16 | 17 | _caller="$(basename -- "$0" | sed -e s/^\-*//)" 18 | 19 | if ! test "$_caller" = "bash"; then 20 | echo "Usage: source $(basename "$0") [profile] [config]" >&2 21 | exit 1 22 | fi 23 | 24 | unset _caller 25 | 26 | # NB! No exit after, only return 27 | 28 | profile="${1:-}" 29 | config_file=$(echo "${2:-${AWS_CONFIG_FILE:-~/.aws/config}}" | sed "s:~:$HOME:" ); 30 | 31 | export config_file 32 | export profile_name=${profile:-$AWS_DEFAULT_PROFILE} 33 | 34 | profiles=($(php -r "$(cat <<"EOL" 35 | $file = getenv('config_file'); 36 | $list = array_keys(array_filter((array) @parse_ini_file($file, true))); 37 | $list = array_map(function($x) {return preg_replace('/^profile\s+/','',$x);}, $list ); 38 | echo $list ? implode(PHP_EOL, $list).PHP_EOL : null; 39 | EOL 40 | )")) 41 | 42 | profile_data=($(php -r "$(cat <<"EOL" 43 | $name = getenv('profile_name'); 44 | $file = getenv('config_file'); 45 | $list = array_filter((array) @parse_ini_file($file, true)); 46 | $keys = array_keys($list); 47 | $vals = array_values($list); 48 | $keys = array_map(function($x) {return preg_replace('/^profile\s+/','',$x);}, $keys); 49 | $key = array_search($name, $keys, true); 50 | echo (false === $key) ? null : implode(PHP_EOL, array($vals[$key]['aws_access_key_id'], $vals[$key]['aws_secret_access_key'])); 51 | EOL 52 | )")) 53 | 54 | updated=0 55 | 56 | if [ -z $profile ]; then 57 | default_profile=$AWS_DEFAULT_PROFILE 58 | elif [[ "-" = $profile ]]; then 59 | updated=3 # turn off 60 | elif [[ "+" = $profile ]]; then 61 | updated=2 # reset default 62 | else 63 | for name in ${profiles[@]}; do 64 | if [[ "x$name" = "x$profile" ]]; then 65 | default_profile="$name" 66 | updated=1 67 | break; 68 | fi 69 | done 70 | 71 | if [ -z $default_profile ]; then 72 | echo -e "${r}ERROR:${x} profile [${y}${profile}${x}] could not be found in [${y}${config_file}${x}] file." >&2 73 | return 1 74 | fi 75 | fi 76 | 77 | if [[ -z $AWS_CONFIG_FILE && $updated -ne 3 ]]; then 78 | echo -e "${g}AWS support was successfully switched on${x}" 79 | fi 80 | 81 | export AWS_ACCESS_KEY="${profile_data[0]}" 82 | export AWS_ACCESS_KEY_ID="${profile_data[0]}" 83 | export AWS_SECRET_KEY="${profile_data[1]}" 84 | export AWS_SECRET_ACCESS_KEY="${profile_data[1]}" 85 | 86 | if [ $updated -eq 1 ]; then 87 | export AWS_CONFIG_FILE=$config_file 88 | export AWS_DEFAULT_PROFILE=$default_profile 89 | 90 | echo -e "${g}Profile was successfully switched${x}" 91 | elif [ $updated -eq 2 ]; then 92 | export AWS_CONFIG_FILE=$config_file 93 | unset AWS_DEFAULT_PROFILE 94 | 95 | echo -e "${y}Profile was successfully reset${x}" 96 | elif [ $updated -eq 3 ]; then 97 | unset AWS_CONFIG_FILE AWS_DEFAULT_PROFILE 98 | unset AWS_ACCESS_KEY AWS_ACCESS_KEY_ID 99 | unset AWS_SECRET_KEY AWS_SECRET_ACCESS_KEY 100 | 101 | echo -e "${y}AWS support was successfully switched off${x}" 102 | else 103 | export AWS_CONFIG_FILE=$config_file 104 | 105 | cat < 6 | # 7 | # For the full copyright and license information, please view the LICENSE 8 | # file that was distributed with this source code. 9 | # 10 | 11 | r="\033[0;31m" # red 12 | w="\033[0;37m" # white 13 | g="\033[0;32m" # green 14 | y="\033[1;33m" # yellow 15 | x="\033[0m" # reset 16 | 17 | if [[ -z $AWS_CONFIG_FILE ]]; then 18 | echo -e "${r}ERROR:${x} Env AWS_CONFIG_FILE must be set" >&2 19 | exit 1 20 | fi 21 | 22 | echo -e "${w}NB!${x} Running ${g}AWS CLI Tools${x} with ${y}${AWS_DEFAULT_PROFILE:-default}${x} profile." >&2 23 | 24 | aws "$@" 25 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | REPOSITORY="https://raw.github.com/jaymecd/aws-profile/master" 4 | DST_PATH=/usr/local/bin 5 | 6 | for F in aws-profile aws-wrapper; do 7 | curl -sSL "${REPOSITORY}/$F" > "${DST_PATH}/$F" 8 | chmod +x "${DST_PATH}/$F" 9 | done 10 | 11 | cat <<"EOL" 12 | # Update ~/.bashrc with this block: 13 | # --------------------------------- 14 | export PATH="/usr/local/bin:$PATH" 15 | 16 | # Amazon AWS Service CLI 17 | complete -C aws_completer aws 18 | alias aws-profile="source aws-profile" 19 | alias aws="aws-wrapper" 20 | # --------------------------------- 21 | EOL 22 | 23 | echo "Done." 24 | --------------------------------------------------------------------------------