EBS Volume Attacher – a simple utility that helps with attaching and switching EBS volumes

amazon ebsI want to share a little utility we wrote, which is basic, but can be quite useful. It can be used for automated tasks that need to attach EBS volumes to EC2 instances, and possibly have these volume replace existing ones. It is based on a lot of the same code as the instance-action script from a previous post.

This utility, which is a python script, can perform a few actions on EBS volumes either attach, switch or switchanddelete:

  • attach” –  will attach an EBS volume to an EC2 instance, this is virtually one command in the basic EC2 toools, but useful to have here as well.
  • switch” –  will stop the instance, wait until it’s in “stopped” state, detach the volume at the specified device (if exists), attaches the new volume and starts the instance.
  • switchanddelete” –  will stop the instance, wait until it’s in “stopped” state, detach the volume at the specified device (if exists) and delete it, attaches the new volume and starts the instance.

These tasks are sometimes needed in reocvery operations when you need to replace an existing EBS volume with one restored from a snapshot. In our solution, Cloud Protection Manager, this functionality is an integral part of the volume recovery process.

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 “switchanddelete” action will delete the previously attached volume. Please make sure it’s data you don’t need or it has a recent snapshot.


Usage:

The help command looks like this:

volume-attacher.py –help
usage: volume-attacher.py [-h] –instance INSTANCE –volume VOLUME –device
DEVICE [–region REGION] [–access_key ACCESS_KEY]
[–secret_key SECRET_KEY] [–force]
{attach,switch,switchanddelete}

Performs actions on an EC2 instance.

positional arguments:
{attach,switch,switchanddelete}

optional arguments:
-h, –help            show this help message and exit
–instance INSTANCE   EC2 instance ID to perform the action on
–volume VOLUME       EBS volume ID to attach
–device DEVICE       device to use when attaching the volume to the
instance
–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 “switchanddelete” action:

volume-attacher.py –access_key AKIAIVKXXXXXXXG3N32Q –secret_key UGSYQL6
P8a9XXXXXXXXXXwnA2GrJ0MOY4Y –volume vol-d582129c –instance i-166f3f38 —
device /dev/sdb –force switchanddelete
Stopping instance i-166f3f38
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Waiting for instance i-166f3f38 to be in ‘stopped’ state
Successfully stopped instance i-166f3f38
Detaching old volume vol-a68313ef from instance i-166f3f38
Waiting for volume vol-a68313ef to be in ‘available’ state
Deleting old volume vol-a68313ef
Successfully attached volume vol-d582129c to instance i-166f3f38
Starting instance i-166f3f38
Waiting for instance i-166f3f38 to be in ‘running’ state
Waiting for instance i-166f3f38 to be in ‘running’ state
Waiting for instance i-166f3f38 to be in ‘running’ state
Waiting for instance i-166f3f38 to be in ‘running’ state
Successfully started instance i-166f3f38
Successfuly attached volume vol-d582129cMore details can be found in the README.txt file.

More details can be found in the README 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:

volume-attacher.py
README.txt

Enjoy…

Share this post →

You might also like: