Simple Amazon EC2 bash autostart script

638

The script below is a simple shell script, that needs more work doing on it to provide greater error checking and to make it more dynamic.

This shell script assumes that you have already created an AWS account, the initial elastic addresses and the ELB from the AWS GUI ( although this could also be included in the script, but I have excluded that part for simplicity).

You will need to download the AWS API tools and the AWS AMI tools for the script to function. It will also be necessary to replace <id> with the correct details for your ‘pk’ and ‘cert’ key that you create from the AWS GUI, and replace the path variables to reflect your own installation.

Currently it will start two EC2 instances using the public images, and assign an already existing Elastic address to each instance and start them in different availability zones for basic resilience, and then put them under the AWS ELB.  This is aimed more at a complete restore in the event that you have a complete failure in both availability zones,.

This is a good way for beginners to understand the internal workings of the AWS EC2 platform and demonstrate proof-of-concept, but not robust enough for complex mission critical deployments.

For more complex requirements and greater robustness, I would recommend using Chef  ‘recipes’ and Knife to provision instances along with the opscode platform.

I would also recommend having a look at the AWS docs and changing the AMI type to m1.small (its cheaper) and picking the public image of your choice.

#/bin/sh
PRIVATE_KEY=pk-<id>.pem
CERT_KEY=cert-<id>.pem
REGION=EU-WEST-1
AMI=ami-a54d67d1
GROUP=defaultgrp
SECURITY_KEY=defaultkey
INSTANCE_TYPE=m1.large
AVAIL_ZONE1=eu-west-1a
AVAIL_ZONE2=eu-west-1b

#STEP1
echo “BEGIN DISASSOC_ADDR”
ELASTIC_ADDR1=`ec2-describe-addresses -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|awk ‘{ print $2 }’|head -1`
ELASTIC_ADDR2=`ec2-describe-addresses -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|awk ‘{ print $2 }’|tail -1`

ec2-disassociate-address -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1 $ELASTIC_ADDR1
ec2-disassociate-address -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1 $ELASTIC_ADDR2
echo “DISASSOCIATING ELASTIC ADDRESS”

#STEP2
#LOAD BALANCER SETTINGS
AWS_LB_TOOLS= /home/userdir/.ec2/ELB_TOOLS/ElasticLoadBalancing-1.0.9.3/bin/
LOAD_BALANCER=/home/userdir.ec2/ELB_TOOLS/ElasticLoadBalancing-1.0.9.3/bin/./elb-describe-lbs  -K /Users/userdir/.ec2/pk-<id>.pem -C /Users/userdir/.ec2/cert-<id>.pem –region=EU-WEST-1|awk ‘{ print $2 }’`
#echo “LOAD BALANCER is” $LOAD_BALANCER

#DEAD INSTANCES
TERMD_INSTANCE1=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep terminated|grep defaultkey|grep INSTANCE|awk ‘{ print $2}’|head -1`
echo $TERMD_INSTANCE1
TERMD_INSTANCE2=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep terminated|grep defaultkey|grep INSTANCE|awk ‘{ print $2 }’|tail -1`
echo $TERMD_INSTANCE2

#REMOVE OLD INSTANCES FROM LOAD BALANCER
echo “Removing old instances from LB”
/Users/userdir/.ec2/ELB_TOOLS/ElasticLoadBalancing-1.0.9.3/bin/./elb-deregister-instances-from-lb $LOAD_BALANCER –instances $TERMD_INSTANCE1,$TERMD_INSTANCE2 -K /Users/userdir/.ec2/pk-<id>.pem -C /Users/userdir/.ec2/<id>.pem –region=EU-WEST-1
echo “Fin rem inst from LB”

#STEP3
#Run up instance in other availability zone
echo “Run up inst1″
ec2-run-instances -K $PRIVATE_KEY -C $CERT_KEY –region=$REGION $AMI -g$GROUP -k $SECURITY_KEY -t $INSTANCE_TYPE -z $AVAIL_ZONE1
#Run up instance in other availability zone
echo “Run up inst2″
ec2-run-instances -K $PRIVATE_KEY -C $CERT_KEY –region=$REGION $AMI -g$GROUP -k $SECURITY_KEY -t $INSTANCE_TYPE -z $AVAIL_ZONE2

RUNNING_INSTANCE1=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $2 }’|head -1`

RUN_INSTANCE1=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $6 }’|head -1`

while  [[ `echo $RUN_INSTANCE1` != “running” [[
do
echo “Waiting for instance to finish startup”

RUN_INSTANCE1=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $6 }’|head -1`
RUNNING_INSTANCE1=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $2 }’|head -1`

done
RUNNING_INSTANCE1=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $2 }’|head -1`

RUN_INSTANCE1=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $6 }’|head -1`

#ELASTIC_ADDR1=`ec2-describe-addresses -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|awk ‘{ print $2 }’|head -1`
echo “BEGIN ASSOC FOR INST1″
echo “this is elastic addr1 $ELASTIC_ADDR1″
echo “this is running instance1 $RUNNING_INSTANCE1″
ec2-associate-address -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1 $ELASTIC_ADDR1 -i $RUNNING_INSTANCE1
echo “Associated Elastic Address for inst1″

RUNNING_INSTANCE2=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $2 }’|tail -1`

RUN_INSTANCE2=`ec2-describe-instances -K pk-<id>C.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $6 }’|tail -1`
#until [ [ `echo ${RUN_INSTANCE2}` = “running” ] ]
#until [ `echo $RUN_INSTANCE2` = “running” ]
while [[ `echo $RUN_INSTANCE2` != “running” [[
do
echo “Waiting for instance to finish startup”

RUN_INSTANCE2=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $6 }’|tail -1`
RUNNING_INSTANCE2=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $2 }’|tail -1`

done
RUNNING_INSTANCE2=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $2 }’|tail -1`

RUN_INSTANCE2=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $6 }’|tail -1`

echo “BEGIN ASSOC FOR INST2″
ec2-associate-address -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1 $ELASTIC_ADDR2 -i $RUNNING_INSTANCE2
echo “ASSOCIATED ELASTIC ADDRESS for inst2″

#LOAD BALANCER SETTINGS
AWS_LB_TOOLS=/Users/userdir/.ec2/ELB_TOOLS/ElasticLoadBalancing-1.0.9.3/bin/
LOAD_BALANCER=`/Users/userdir/.ec2/ELB_TOOLS/ElasticLoadBalancing-1.0.9.3/bin/./elb-describe-lbs -K /Users/userdir/.ec2/pk-<id>.pem -C /Users/userdir/.ec2/cert-<id>.pem –region=EU-WEST-1|awk ‘{ print $2 }’`

LB_HOST1=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $2 }’|head -1`
LB_HOST2=`ec2-describe-instances -K pk-<id>.pem -C cert-<id>.pem –region=EU-WEST-1|grep running|grep defaultkey|grep INSTANCE|awk ‘{ print $2 }’|tail -1`

#ADD NEW INSTANCES INTO LOAD BALANCER
/Users/userdir/.ec2/ELB_TOOLS/ElasticLoadBalancing-1.0.9.3/bin/./elb-register-instances-with-lb $LOAD_BALANCER –instances $LB_HOST1,$LB_HOST2 -K /Users/userdir/.ec2/pk-<id>.pem -C /Users/userdir.ec2/cert-<id>.pem –region=EU-WEST-1