Manage your AWS credentials for multiple accounts

Posted . Visible to the public.

Create a directory mkdir ~/.aws

Initialise git repository cd ~/.aws && git init

Create a git branch with a name you want (e.g. development for the aws development account credentials).

Add AWS credential file .aws_credentials:

AWSAccessKeyId=ABCDEFG1234
AWSSecretKey=4321GFEDCBA

Also add your EC2 cert and private key file.
You can add other AWS account depending files like .fog or .guignol.yml too.

Create symlinks for some config files like .aws_credentials and .fog:

ln -s ~/.aws/.aws_credentials ~/.aws_credentials
ln -s ~/.aws/.fog ~/.fog

If you want to manage a new account you just have to add a new git branch.

Add this to your bashrc:

export EC2_PRIVATE_KEY=~/.aws/pk-ec2.pem
export EC2_CERT=~/.aws/cert-ec2.pem
export AWS_CREDENTIAL_FILE=~/.aws/aws-credentials

Perhaps you also want to set a default region and the JAVA_HOME:

export EC2_REGION=eu-west-1
export EC2_URL=https://eu-west-1.ec2.amazonaws.com
export JAVA_HOME=/usr

Add a bash script somewhere in your $PATH (I called it am):

#!/bin/bash
if [[ $1 == "list" ]]; then
  cd ~/.aws && git branch -l
elif [[ $1 != "" && `cd ~/.aws && git branch | grep $1` ]]; then
  cd ~/.aws && git checkout $1 > /dev/null
  echo "Ready to Manage $1!"
else
  echo "$1 is not a valid ec2 branch"
fi

Now you can easly manage different AWS accounts and choose it with am branchname.

Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Kim Klotz to makandra dev (2013-04-23 12:15)