You are storing data on an EC2 instance and you would like that data to persist even if the instance fails.
Use the create_volume
method and other EBS-related
methods to create a volume and attach it to an existing instance.
All EC2 instances have a root volume associated with them. This is where the operating system is installed. The size of this root volume depends on how the image was bundled and whether the corresponding AMI is S3-backed or EBS-backed.
In addition, all EC2 instance types except the
t1.micro
have dedicated instance storage. The number of
volumes and total amount of instance storage varies depending on the
instance type, but all instance storage has one property in common: it
is ephemeral. When you terminate the instance (or when it fails for some
reason), all of the instance storage goes away and anything you have
stored there will be lost.
To provide persistent storage (that is, storage that exists independent of an instance), EC2 provides Elastic Block Storage (EBS). EBS allows you to create volumes of any size (from 1 GB to 1 TB) and attach the volumes to any EC2 instance. If that instance fails for any reason, the volume can be reattached to a replacement instance and the data will then be available to that new instance.
EBS volumes have about the same reliability you would expect from a RAID device, so while failures are not common, they can happen. We will discuss ways to back up your EBS volume in Back Up Your EBS Volumes.
Example 2-8. Attach a Volume
import boto import time def create_and_attach_volume(instance, volume_size, device_name): """ Create a new EBS volume and attach it to the instance. instance The instance object representing the instance to which the volume will be attached. volume_size The size (in GB) of the new volume. device_name The device name to which the new volume will be referred to on the instance. """ ec2 = instance.connection # Determine the Availability Zone of the instance azone = instance.placement volume = ec2.create_volume(volume_size, azone) # Wait for the volume to be created. while volume.status != 'available': time.sleep(5) volume.update() volume.attach(instance.id, device_name) return volume