Getting Started with EC2

09 Apr 2008

Wow is EC2 fussy. You know this already. Such is life when using public-key encryption. if you are just getting started, the following greatly simplifies the situation. It won't handle multiple accounts or multiple instances, but sometimes one is all you need.

First, go through the stock EC2 tutorial. And then compile all the keys and stuff into one spot like so:

export S3_PUBLIC='your s3 public key'
export S3_PRIVATE='your s3/private key'
export EC2_PRIVATE_KEY='./pk-YOURCERT.pem'  # somewhere
export EC2_CERT='./cert-YOURCERT.pem'  # somewhere

export EC2_HOME='./ec2-api-tools-1.3-19403'  # OR SOMETHING
export PATH=$EC2_HOME/bin:$PATH

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/home

export AMI='ami-226e8b4b'   # or something

export EC2_SSH_KEY=yourname-keypair

Ok, now the following functions make it simple to start stop and login

# VERSION 3 -- now with ec2do
# VERSION 2 -- now with push/pull

function ec2start {
    ec2-run-instances $AMI -k $EC2_SSH_KEY

function ec2list {

function ec2host {
   # it's one line below
    ec2-describe-instances | grep INSTANCE | grep | head -n 1 | awk '{print $4}'

function ec2id {
   # it's one line below
    ec2-describe-instances | grep INSTANCE | grep | head -n 1 | awk '{print $2}'

function ec2stop {
    echo "Stopping..."
    ec2-terminate-instances `ec2id`

function ec2login {
    ssh -i id_rsa-$EC2_SSH_KEY root@`ec2host`

function ec2push {
    scp -i id_rsa-$EC2_SSH_KEY $1 root@`ec2host`:~/

function ec2pull {
    scp -i id_rsa-$EC2_SSH_KEY root@`ec2host`:$1 .

function ec2do {
       ssh -i id_rsa-$EC2_SSH_KEY root@`ec2host` $@

function ec2help {
    echo "haha"
    echo ""
    echo "ec2list  -- alias for ec2-describe-instances"
    echo "ec2start -- starts one instance"
    echo "ec2stop  -- stops an instance"
    echo "ec2id    -- lists the reservation id of a running instance"
    echo "ec2host  -- lists the host of a running instance"
    echo "ec2login -- log in to running instance"
    echo "ec2push localfile  -- push a file to remote homedir"
    echo "ec2pull remote -- pull a remote file to local current dir"
    echo "ec2do cmds -- execute a command remotely"

Put all of that into your profile, and you'll be all set. Enjoy..