Nice and Simple AWS EC2 Start Stop Script – Python

Here is a nice and simple AWS EC2 start stop script written in Python and run from the command line. The parameters are just start|stop and the name of your instance.

I’ve written it so it uses the tag Name instead of requiring the specific instance ID. The script’s ideal usage may be to schedule the start|stop of an instance at particular times of the day. So you might have a test server you use during 10AM and 3PM Monday to Friday. With this script you could setup a scheduled task on a remote computer to start|stop the instance based on your schedule, saving you money!

Prerequisites

In order to run this script you’ll need to have Python and the AWS library Boto installed on your computer. Remember this can be any computer with an internet connection (ideally not the EC2 instance you plan on stopping/starting). The script will run on Linux and Windows.

I’ve previously written guides on installing Python and Boto into Linux environments. If you use Windows, you will find many guides stepping you through this process.

My Python/Boto install guides:

CentOS and Amazon AMI

Ubuntu 13.04

Script Usage

For the script to work you’ll need to know 3 things:

  • AWS ACCESS KEY ID
  • AWS SECRET ACCESS KEY
  • Region Code

You can find your access keys from your security credentials page: https://portal.aws.amazon.com/gp/aws/securityCredentials

You can find your Region Code in the table on this page: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html

Simply save the python code into a file and name it whatever you like, I’ve chosen ec2-startstop.py. Make the adjusts to suit your access keys and region code, then set the script to executable (chmod +x ec2-startstop.py), and simply run it like this:

 The Script

 

  9 comments

  1. Shohel   •  

    Very handy script! Thanks :)

  2. Vijay   •  

    Can you provide a script for auto start and stop of amazon instances from 8 am IST to 5 pm IST daily from monday to friday.Please help me on the above

  3. Samuel Zuniga Vanoye   •  

    Very useful script, thanks for sharing

  4. tomer   •  

    Very nice and elegant

  5. Sameer   •  

    Thanks for the script its awesome however running this i get this error
    line 46, in
    inst = conn.get_all_instances(filters={‘tag:Name’: myinstance})[0].instances[0]
    AttributeError: ‘NoneType’ object has no attribute ‘get_all_instances’

    Thanks
    Sameer

    • Ajay   •  

      I was getting the same error but after correcting the REGION the script running smoothly.

  6. Michael Young   •  

    I am getting same error on this code:
    File “ec2start.py”, line 46, in
    inst = conn.get_all_instances(filters={‘tag:Name’: myinstance})[0].instances[0]
    AttributeError: ‘NoneType’ object has no attribute ‘get_all_instances’

  7. On the EC2 Management Console, click Launch Instance to start the process to provision a micro EC2 instance. I recommend to select the latest Amazon Linux AMI, which has AWS tools, python and boto pre-installed.

  8. AWinssoita   •  

    First things first, you should set a set target date when ever you’ll completely quit, including several weeks from now. He is content, along with the complete opposite of our thoughts later years has in store for us.

Leave a Reply

Your email address will not be published. Required fields are marked *


3 × = three

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