Advanced customization of Amazon EC2 instances — attaching snapshots
In some cases there is a need to automatically attach an EBS volume created from a snapshot to a newly-created node. The script (cm-attach-ec2-snapshot) attached to the end of this article can help to solve this, or a similiar, task.
In order to use the script, some changes should be made to the software image of the node.
yum install
First of all, some packages should be installed:yum install cmdaemon-pythonbinding python-boto cluster-tools
ebs volume configuration file
Also, a snapshot configuration file /etc/ebs_volume.conf should be created
Sample contents:[DEFAULT]
provider_name=Amazon
snapshot_id=snap-e12345
volume_device=/dev/sdh
Here:
provider_name — name of the cloud provider in Bright Cluster Manager
snapshot_id — the ID of a snapshot to be used for EBS volume creation
volume_device — the device name to be attached (e.g. /dev/sdf). Note that the real device name will probably differ from this value, and will be printed by a script at the end of its execution.
script placed in software image, path to script appended to rc.local
The script itself (cm-attach-ec2-snapshot) should be placed in the software image, and a path to it should be appended to /etc/rc.local
Sample /etc/rc.local file:#!/bin/sh -e
/usr/local/bin/cm-attach-ec2-snapshot
exit 0
finalize script instance metadata grab
The finalize script for the node should also be modified to include the following command:wget -q -O /localdisk/etc/instance_metadata http://169.254.169.254/latest/dynamic/instance-identity/document
After all is set, when the machine starts the script it will create an EBS volume from a snapshot id specified in /etc/ebs_volume.conf, attach it to a running instance, and print the new block devices.