Monday, March 23, 2009

Creating and using Amazon Machine Images and Amazon Elastic Block Storage

Let's say you have managed to get an instance running SQL Server 2005 on a Windows 2003 operating system up and running. You are running concurrent CRUD operations against a database, but you decide you need to install a service package and restart the instance. That should be no problem right?

Wrong!

When you terminate a running instance, any data changes made during the session are lost unless they were saved on Amazon Elastic Block Store (henceforth referred to as EBS). The only way to save any data on your instance that is not stored on EBS is by bundling the image. Bundling an image means saving the instance and it's current configuration to Amazons's Simple Storage Service (henceforth referred to as S3). Typically what you would do is to create your image, test it and save it again as part of an iterative process moving towards perfection. Once all the necessary applications and configuration settings have been made and you have made sure everything works, you should only need to save production data, and that should be stored on an EBS volume. One of the biggest advantages with EBS volumes is that if your image becomes corrupt, you can simply attach the EBS volume to a new image and retrieve the data. You can also create snapshots of EBS volumes.

The easiest way to create and save an image is to launch one of the images in Amazon's public image directory, install any additional software and service packages, then bundle it using the management console or Elasticfox, unless you are John Wayne or somebody who insists on doing it the hard way by using Amazon's command line tools. The image you bundle can be registered as either private or public. A public image can be accessed and launched by anyone with an AWS account. You can also create images on your own machine and upload to Amazon. In this posting however, we will focus on creating new images from running instances initalized using existing images already registered with AWS.


To create an image from a running instance using Elasticfox:

1. Select the 'Instances' tab

2. Right click the instance and select the 'Bundle into an AMI' option.

3. Type a bucket name and an image name for your S3 folder (a new bucket will be created if a bucket with the specified name does not exist, then click ok.

4.Click the 'Bundle Task' tab and you will see the progress of the bundle task you just created. It can take a while so be patient. Bundling actually consists of two steps, bundling or packaging the image, then saving it to S3. The progress will be displayed in the Bundle State column. Once the process is finished the Bundle State column will display as 'complete'.

5. Once the 'complete' status is reached, you need to right click the image file in the Bundle Tasks view and select 'Register AMI' to register it before you can use it. Adding a descriptive tag is optional but useful if you need to keep track of a lot of images.

6. The image you created is now registered and will appear among your images with the Visibility value set to 'Private'.

7. To launch the image you created, right click and select the 'Launch instance(s) of this AMI' option.



As mentioned above you need to have an EBS volume to persist data between sessions. So how do you create an EBS volume?

To create an EBS volume using Elasticfox:

1. Click on the Volumes and Snapshots tab, then click the add(+) button to create your EBS volume.

2.Type desired size (anything between 1 GB and 1 TB), add a tag, click create, and Bob's yer uncle.

Attaching the EBS volume is even simpler:

1.In Elasticfox, right click your running instance and select 'Attach an EBS volume'.

2.In the popup, select the id of the volume you want to attach and click 'Attach'.

Assuming that you attached the EBS volume to a Windows instance, all you need to do now is to log on to your running image, then go to Administrative Tools, select Computer Management and format the volume. Note that your instance and your image must be running in the same availability zone, otherwise you will get an error.

The C# library also offers functions for handling EBS volumes. The following code example shows how to create an EBS volume and attach it to a running instance (for an example on how to launch an instance, see my previous post Initializing and Controlling your instance on EC2):

First of all, make sure you are running your instance in the same availability zone
as the EBS volume. If the volume is located in the us-east-1a zone, your instance must be launched there as well by setting the Placement property for an instance of the RunInstancesRequest class:

Placement p = new Placement();
p.AvailabilityZone = "us-east-1a";
request.Placement = p;

Assuming that we have launched the instance successfully, we can now attach our EBS volume (for initialization of the service class see Initializing and Controlling your instance on EC2):

AttachVolumeRequest request = new AttachVolumeRequest();
request.VolumeId = "vol-e38d6c8a";
request.InstanceId = "i-a05e3bc9";
request.Device = "xvdg";
AttachVolumeResponse response = service.AttachVolume(request);

If the request was successful, the response.Attachment.Status property will be set to 'attaching'. You can also check the result by opening Elasticfox and clicking the Volumes and Snaphots tab and checking the Attachment Status field of the EBS volume specified in the function call. To monitor your instances and volumes use the DescribeInstancesRequest and DescribeVolumesRequest classes, but more about that some other time. Sampai nanti as they say in Indonesian.

No comments:

Post a Comment