nifty ec2 startup/shutdown scripts

It became incrementally annoying to have to check whether I had an ec2 instance running, booting it up if it wasn’t, copying and pasting the appropriate instance ID to ec2-start-instances, repeatedly invoking ec2-describe-instances to check if it was up, stopping it, copying and pasting… well you get the idea.

These simple scripts will automate the above process, and hopefully save you from typing unnecessarily into your console.

I saved this file as start-ec2 under my ~/.ec2 directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#! /bin/bash                                                                                                                                                
#show instances                                                                                                                                             
inst=$(ec2-describe-instances | cut -f 3 | grep ^i-)
echo "Instance ID: " $inst
 
#start instance                                                                                                                                             
echo "booting up..."
status=""
ec2start $inst
while [ "$status" != "running" ]; do
    status=$(ec2-describe-instances | cut -f 6 | grep run)
    echo "booting up..."
    sleep 5
    if [ "$status" == "running" ]; then
        echo $inst " has succesfully booted!"
        break
    fi
done
 
#when instance is running, associate elastic ip to it      
eIP="50.16.xx.xx"                                                                                                 
echo "Associating Elastic IP 50.16.xx.xx to " $inst
ec2-associate-address -i $inst $eIP

Don’t forget to make it executable. Run it and verify the output:

bash$ chmod 755 start-ec2
bash$ ./start-ec2
Instance ID:  i-x43xxxx
booting up...
INSTANCE	i-x43bxxxx	stopped	pending
booting up...
booting up...
booting up...
i-x43bxxxx  has succesfully booted!

And my shutdown script stop-ec2 is defined as follows (again, don’t forget to chmod 755 it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! /bin/bash                                                                                                                                                
inst=$(ec2-describe-instances | cut -f 3 | grep ^i-)
status=""
 
ec2stop $inst
while [ "$status" != "stopped" ]; do
      status=$(ec2-describe-instances | cut -f 6 | grep stop)
      echo "shutting down..."
      sleep 5
      if [ "$status" == "stopped" ]; then
         echo $inst " has " $status
         break
      fi
done

Here is some sample output from the above script:

bash$ ./stop-ec2
INSTANCE	i-x43bxxxx	running	stopping
shutting down...
shutting down...
shutting down...
i-x43bxxxx  has  stopped

Hope all this was as useful to you as it was to me!

characters have been x’ed for privacy purposes

2 thoughts on “nifty ec2 startup/shutdown scripts

  1. Hello:

    Simple and excellent solution.

    But, if you have more than one instance you should change your stop-ec2 as

    inst=$(ec2-describe-instances | cut -f 3 | grep ^i-)
    echo “Instance ID: ” $inst

    while [ “$status” != “stopped” ]; do
    status=$(ec2-describe-instances $inst | cut -f 6 | grep stop)

    if not you will get the status of all your instances and your $status.
    and your script will never end.

    Jose

Leave a Reply

Your email address will not be published.


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>