Wednesday, March 18, 2009

Initializing and controlling your own instance on EC2

When it comes to cloud computing, there are two paths you can go by as Robert Plant sang in 1971: One is SaaS, and the other is virtualization. Amazon's EC2 offering uses the latter. A virtual machine is a software implementation of a computer that executes programs like a real machine. Amazon and it's two competitors GoGrid and Flexiscale all use Xen,an open source virtual machine monitor. This reduces the risk for vendor lock-in and a customer can also download his image and run it in his own Xen environment. This posting focuses on how to get started with Amazon EC2.

First of all you need to open an account, if you have ever bought something from Amazon you are already halfway there since Amazon already have the billing info, otherwise you need to sign up for AWS (Amazon Web Services) which includes EC2 and a bunch of other stuff that I will write about in another posting. Secondly, you need to sign up for the EC2 service, and thirdly you need to obtain a private and a public key or a X.509 certificate which both are provided by Amazon. In the case of the latter you can also bring your own certificate.

Once you have finished registration you are ready to launch and control your own instance using one or several of the tools provided by Amazon:

For those who want to avoid installing non-Microsoft products, the best alternative is of course the EC2 console but I prefer using Elasticfox which is a plugin for Firefox although the difference in functionality between the two is rather insignificant. For those of you who are into self-flagellation, download the command line tool. Here's how you launch an instance with Elasticfox:



Amazon also provides libraries for several programming languages. In this post I will show how to launch and terminate images using the C# Library for EC2. The library is using the .NET 2.0 framework and wraps the Amazon APIs. The C# library includes mock-up functionality which simulates responses from the Amazon EC2 service. The mock-up is helpful if you want to make sure your code does not contain bugs before you go live.

To initialize two instances of the same image, in Visual Studio 2005 or 2008, set a reference to the C# Amazon.EC2 library and add the following using statements:
......
using Amazon.EC2;
using Amazon.EC2.Model;
......
Initialize an instance of the AmazonEC2 class passing the access key and the secret access key as string variables:

AmazonEC2 service = new AmazonEC2Client(accessKeyId, secretAccessKey);

Initialize an instance of the RunInstanceRequest class:
......
RunInstancesRequest request = new RunInstancesRequest();
// set request parameters here
request.ImageId = "your image id goes here";
//Set allowed number of instances:
request.MinCount = 1;
request.MaxCount = 2;
//If we are using SQL Server and Windows 2003 we need the
//m1.large instance type:
request.InstanceType = "m1.large";
RunInstancesResponse response = service.RunInstances(request);
....

In order to stop an instance you simply create an instance of the TerminateInstancesRequest class:
TerminateInstancesRequest request = new TerminateInstancesRequest();
//Set the id to the instance you want to terminate, InstanceID is a collection
//so you can pass more than one instance id:
request.InstanceId.Add("your instance id goes here");
TerminateInstancesResponse response = service.TerminateInstances(request);

The code examples above just show a fraction of all the things you can do with the EC2 API. Remember that bugs can be costly when working with EC2, you don't want to be charged for starting more instances than you actually planned. Sample code can be found here.

No comments:

Post a Comment