Ben G
Ben G

Reputation: 26751

Managing multiple AWS accounts on the same computer

I have multiple AWS accounts, and depending on which project directory I'm in I want to use a different one when I type commands into the AWS CLI.

I'm aware that the AWS credentials can be passed in via environmental variables, so I was thinking that one solution would be to make it set AWS_CONFIG_FILE based on which directory it's in, but I'm not sure how to do that.

Using Mac OS X, The AWS version is aws-cli/1.0.0 Python/2.7.1 Darwin/11.4.2, and I'm doing this all for the purpose of utilize AWS in a Rails 4 app.

Upvotes: 2

Views: 866

Answers (2)

Bharat Jain
Bharat Jain

Reputation: 664

I think you can create an alias to aws command, which will export the AWS_CONFIG_FILE variable depending on the directory you are in. Something like following (bash) may work.

First create following shell script, lets call it match.sh and put it in /home/user/

export AWS_CONFIG_FILE=`if [[ "$PWD" =~ "MATCH" ]]; then echo "ABC"; else echo "DEF"; fi`
aws "$@"

Now define alias in ~/.bashrc script

alias awsdirbased="/home/user/match.sh $@"

Now whenever you wanna run "aws" comand instead run "awsdirbased" and it should work

Upvotes: 1

dcro
dcro

Reputation: 13649

I recommend using different profiles in the configuration file, and just specify the profile that you want with:

aws --profile <your-profile> <command> <subcommand> [parameters]

If you don't want to type the profile for each command, just run:

export AWS_DEFAULT_PROFILE=<your-profile>

before a group of commands.

If you want to somehow automate the process of setting that environment variable when you change to a directory in your terminal, see Dynamic environment variables in Linux?

Upvotes: 3

Related Questions