fbpx

Instance Action – a simple utility that helps with daily EC2 instance tasks

ec2 instance cpm aws
Share This Post

ec2 instance cpm awsI want to share a little utility we wrote, which is basic, but can be quite useful. We and some of our customers use it for automated tasks that need to start, stop and terminate EC2 instances and wait until the operation is completed.
This utility, which is a python script, can perform a few actions on existing EC2 instances, either start, stop, terminate or terminate_with_vols:

  • start” –  will start the instance and wait until it’s in “running” state.
  • “stop” –  will stop the instance and wait until it’s in “stopped” state.
  • terminate” –  will terminate the instance and wait until it’s in “terminated” state.
  • terminate_with_vols” – will terminate the instance, wait until it’s in “terminated” state, and then delete all volumes that were attached to the instance and have the “delete_on_termination” flag turned off (others will be deleted automatically upon termination anyway).

The script is written in Python and will work on Linux or Windows, as long as Python 2.7.3 or newer (not Python 3) is installed, and the “boto” library is installed as well. Instructions to install boto can be found here: http://boto.readthedocs.org/en/latest/getting_started.html#installing-boto
(Or simply download it from https://pypi.python.org/pypi/boto then open the tarball, go in the installation folder and type: python setup.py install)
The script is well documented. It can get the AWS credentials from command line or set defaults in the script itself. If the script is run from within an instance that has a proper IAM role, then no credentials are needed at all.
Unless the –force option is used, Instance-action will ask for confirmation before every action.


Please be careful using it: The “terminate” and “terminate_with_vols” actions can lose you data forever. Please make sure it’s data you don’t need or is backed up elsewhere.


Usage:
The help command looks like this:

instance_action.py –help
usage: instance_action.py [-h] –instance INSTANCE [–region REGION]
[–access_key ACCESS_KEY] [–secret_key SECRET_KEY]
[–force]
{start,stop,terminate,terminate_with_vols}

Performs actions on an EC2 instance.
positional arguments:
{start,stop,terminate,terminate_with_vols}
optional arguments:
  -h, –help            show this help message and exit
  –instance INSTANCE   EC2 instance ID to perform the action on
  –region REGION       AWS region the instance is in. If missing default is
                        used
  –access_key ACCESS_KEY
                        AWS API access key. If missing default is used
  –secret_key SECRET_KEY
                        AWS API secret key. If missing default is used
  –force               If set, this flag will prevent confirmation for the
                        action
Example of a “terminate” action:

>instance_action.py –access_key AKIAIVK2OXXXXXXXXXXX –secret_key UGSYQL
6XXXXXXXXXXXXXXXXXXXXXJ0MOY4Y –region us-east-1 –instance i-48432f1b terminate
Confirm terminating instance i-48432f1b (region: us-east-1)[yes/no] y
Terminating instance i-48432f1b
Waiting for instance i-48432f1b to be in ‘terminated’ state
Waiting for instance i-48432f1b to be in ‘terminated’ state
Waiting for instance i-48432f1b to be in ‘terminated’ state
Waiting for instance i-48432f1b to be in ‘terminated’ state
Waiting for instance i-48432f1b to be in ‘terminated’ state
Waiting for instance i-48432f1b to be in ‘terminated’ state
Waiting for instance i-48432f1b to be in ‘terminated’ state
Waiting for instance i-48432f1b to be in ‘terminated’ state
Successfully terminated instance i-48432f1b

More details can be found in the README.txt file.


Disclaimer:
This script is given as-is with no warranty. You can use it for any purpose, distribute it or modify it, just don’t come complaining to us…


Download:
instance_action.py
README.txt
Enjoy…]]>

Next step

The easier way to recover cloud workloads

Allowed us to save over $1 million in the management of AWS EBS snapshots...

Try N2WS for Free