Skip to content

Instantly share code, notes, and snippets.

@sovcik
Created November 22, 2010 10:47
Show Gist options
  • Save sovcik/709802 to your computer and use it in GitHub Desktop.
Save sovcik/709802 to your computer and use it in GitHub Desktop.
Script for account/identity switching while using Amazon AWS Cloud tools
#!/bin/bash
###############################################
#
# Script for switching identities while working with Amazon AWS.
# Sets environment variables used by AWS tools.
#
# Copyright (c) 2010 by Jozef Sovcik, http://www.vanilladesk.com
#
#---------------------------------------------
# How to use:
# 1) create folder 'aws' in your home folder
# $ mkdir aws
# 2) in folder 'aws' create sub-folder for each identity you
# would like to use (identity=AWS account or user of AWS account)
# $ mkdir account1
# 3) for each identity create/copy necessary files
# a) identity file 'aws-credentials'
# (should contain access key id and secret access key)
# see http://docs.amazonwebservices.com/IAM/2010-05-08/GettingStartedGuide/
# b) private key 'pk-current.pem'
# (idea: generate a key and create 'pk-current.pem' as symlink to it)
# c) certificate file 'cert-current.pem'
# (idea: generate a certificate and create 'cert-current.pem' as symlink to it)
# 4) call this script using shell command 'source' in order to modify
# existing environment with
# $ source iam-switch.sh <identity folder name>
# 5) calling this script without any paramaters only shows the current identity
#
###############################################
function set_aws_id() {
local awsid="$1"
if [ ! "$awsid" ]; then
echo "AWS Identity Switch"
echo "Current AWS Identity: $AWSI_NAME"
return 0
fi
local awsid_fld=~/aws/$awsid
if [ ! -d "$awsid_fld" ]; then
echo "Error: Folder $awsid_fld does not exist."
return 1
fi
export EC2_PRIVATE_KEY=$awsid_fld/pk-current.pem
export EC2_CERT=$awsid_fld/cert-current.pem
export AWS_CREDENTIAL_FILE=$awsid_fld/aws-credentials
export AWSI_NAME=$awsid
echo "New AWS Identity: $AWSI_NAME"
return 0
}
set_aws_id $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment