A Quick Way to Manage Multiple Amazon AWS Accounts

I’m often switching between different Amazon AWS accounts. So to speed things along I’ve set up some simple scripts to easily switch between accounts while at the command line

On Linux (aws_setup):

<code>#!/bin/bash
export EC2_URL=https://eu-west-1.ec2.amazonaws.com
export EC2_CERT=/home/john/cert-12345678901234567890123456789012.pem
export EC2_PRIVATE_KEY=/home/john/pk-12345678901234567890123456789012.pem</code>

Once saved, run chmod +x aws_setup
On Windows (aws_setup.cmd):

<code>SET EC2_URL=https://eu-west-1.ec2.amazonaws.com
SET EC2_CERT=C:\Users\John\cert-12345678901234567890123456789012.pem
SET EC2_PRIVATE_KEY=C:\Users\John\pk-12345678901234567890123456789012.pem</code>

Save as many files with the paths to your various AWS key pairs, and simply run the script from within a terminal before using the ec2 tools.

NOTE, when you run the scripts on Linux (and I guess Macs), add an extra dot before you run it. This will allow the environment variable to persist when the script ends.

<code>. ./aws_setup</code>

Commands to get started with Amazon EC2

Here are some simple commands to get you started with using Amazon EC2:

ec2dim
Displays your account’s AMIs that are on Amazon

ec2run ami-XXXXXXXX
This launches your Amazon EC2 instance. Use your own or one of Amazon’s AMI IDs

ec2din
Describes your running instances with AMI and instance IDs

ec2dvol
Lists your account’s volumes and their status

ec2attvol vol-XXXXXXXX -i i-XXXXXXXX -d xvdf
Attaches an Elastic Block Storage device to your running instance

ec2detvol i-XXXXXXXX
Detaches the storage volume from an instance (use -f to force)

ec2stop i-XXXXXXXX
Stops a running instance so that you can perform maintenance on it

ec2minatt i-XXXXXXXX
Modifies an instance attribute such as the instance type

ec2start i-XXXXXXXX
Resumes your stopped instance

ec2kill i-XXXXXXXX
Terminates your running instance