Click here to Skip to main content
15,867,141 members
Articles / Web Development / ASP.NET

Easy Amazon EC2

Rate me:
Please Sign up or sign in to vote.
4.94/5 (13 votes)
30 Jun 2016CPOL4 min read 61.1K   1.2K   45   10
Create virtual machines in the cloud using Amazon EC2

Create Machines in the Cloud

If you are remotely involved in IT, then I'm sure you've heard of the latest hype to help save humanity: Cloud Computing. But it's a lot more than hype. Cloud computing is a major change in IT that has the potential to make your business operate faster, smarter and at lower cost.

To avoid confusion, below is my definition of cloud computing:

  1. On-demand: Services must be available by request, at any time and anywhere.
  2. Self-service: Users of cloud services must manage their own workflows. It's not cloud computing if a person needs to fill out extensive forms, submit requests or beg to make the service work.
  3. Granular: The service must bill solely for usage. In many cases, usage is billed on an hourly basis.

Amazon EC2 is one of the most mature platform cloud computing services available. This article will show you how to create EC2 machines in the cloud and connect to them using RDP. The code is written in ASP.NET, to help companies that ultimately wish to write their own issuance frameworks as browser based solutions.

Background

Amazon EC2 is a mature cloud computing platform. It allows you to provision your own Windows, Linux or Unix machines in the cloud, and can bill as little as 8 cents/hour for usage. The Amazon service is also exposed as a SOAP API, allowing you to create and manage infrastructure from your own C# applications.

While the SOAP interface is relatively easy to use, I have found that many people struggle with the basic concepts involved. The most common issues people have:

  • Confusion about private keys and how to use them.
  • Difficulties in retrieving passwords to log onto a Windows instance.
  • Access issues, almost always due to misconfiguration of security groups.
  • Challenges in data format conversions in order to read a Windows password and view machine logs.

The rest of this article will discuss a very basic wrapper and ASP.NET application that will help you to launch your own Amazon EC2 machines through the browser and connect to them via RDP.

Using the Code

The basics of launching your machine in the cloud.

1. Validate your Connection

Download the Amazon SDK and add it as a project reference. The SDK provides a wrapper to expose the SOAP interface as a set of C# method calls. Download it from here.

To use Amazon, you will need to initialize the AmazonEC2 service call with your Access Key and Secret Access Key. These parameters are effectively your username and password for web service access. Use this link to help with EC2 registration and access keys:

C#
// Initialize the service
AmazonEC2 service = new AmazonEC2Client(AWSAccessKey, AWSSecretAccessKey);
// Validate the service with a basic EC2 query
DescribeImagesRequest request = new DescribeImagesRequest();
request.WithOwner("self");
service.DescribeImages(request);

2. Configure Access Rules

Each machine you launch is dependent on:

  • Key pairs: This is a public/private key pair that you generate. The public key will be used by Amazon to encrypt your image and Windows password, while the private key is used by you to decrypt the Windows password. This is very important! If you lose the private key, then no-one can recover an EC2 instance password from the cloud.
  • Security groups: These are the firewall access rules for your environment.

Below is an example of Key Pair generation. Note that it returns a String containing your Private Key. As above, do not lose this value.

C#
CreateKeyPairRequest request = new CreateKeyPairRequest();
request.KeyName = keyName;
CreateKeyPairResponse response = service.CreateKeyPair(request);
return response.CreateKeyPairResult.KeyPair.KeyMaterial;

3. Launch your Machine

Launching EC2 instances is very simply. You will need your key pair, security group and machine identifier. What's a machine identifier? Amazon gives a random ID to each image available in the cloud. You can search for images of interest over here.

Once you launch an instance, you will receive back an instance identifier. This value can be used to retrieve future information about your instance, such as DNS address and machine state.

Note: It can take quite some time for your instance to be fully available. Machines get configured and rebooted as part of a launch, and during this time, you'll just have to sit and wait...

C#
RunInstancesRequest request = new RunInstancesRequest();
request.ImageId = ami;
request.MaxCount = 1;
request.MinCount = 1;
request.KeyName = keyName;
request.SecurityGroup = securityGroups;
request.InstanceType = type;

RunInstancesResponse response = service.RunInstances(request);
RunningInstance runningInstance = 
	response.RunInstancesResult.Reservation.RunningInstance[0];	

4. Connect to your Instance

You would think this is the easy part, but it's not:

  • The instance takes time to boot up and there's not a lot of information available during this time.
  • You can try to retrieve some log information with GetConsoleOutput. You will need to call this method repeatedly to make sure you've got the latest log data (or any data).
  • The Windows password is encrpyted. When available, you will need to retrieve it with GetPasswordData and then decrypt it with the PEM private key that you generated earlier.

The attached code also allows you to connect to the instance with an RDP file that's generated on the fly:

C#
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.ContentType = "Content-Type=application/x-rdp rdp;charset=ISO-8859-1";
Response.AddHeader("Content-Length", content.Length.ToString());
Response.Write(content);
Response.End();

Points of Interest

Being able to generate virtual machines through a basic browser interface can be of significant benefit to a company. The startup I work for, CloudSlice.io, builds Virtual Lab Management environments using code similar to the above. They extend the Amazon EC2 environment to create a self-service, multi-user and sharable cloud environment.

History

  • 12 Aug 2010: Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
GregoryW16-Jul-13 1:53
GregoryW16-Jul-13 1:53 
QuestionHow to detect the machine is reachable already? Pin
Dioksin29-Mar-13 0:20
Dioksin29-Mar-13 0:20 
GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 8:28
professionalKanasz Robert27-Sep-12 8:28 
QuestionNice article! Pin
Jay Macro7-Sep-12 11:46
Jay Macro7-Sep-12 11:46 
GeneralMy vote of 5 Pin
Abinash Bishoyi1-Aug-12 1:55
Abinash Bishoyi1-Aug-12 1:55 
GeneralExcellent, super work. and one question Pin
imak19-Aug-10 5:49
imak19-Aug-10 5:49 
GeneralRe: Excellent, super work. and one question Pin
Simon S Ellis19-Aug-10 9:37
Simon S Ellis19-Aug-10 9:37 
GeneralMy vote of 5 Pin
Member 724777613-Aug-10 12:01
Member 724777613-Aug-10 12:01 
GeneralMy vote 5 Pin
redjes12-Aug-10 20:43
redjes12-Aug-10 20:43 
thank so much Smile | :) Laugh | :laugh: Wink | ;)
red

GeneralMy vote of 5 Pin
defwebserver12-Aug-10 7:03
defwebserver12-Aug-10 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.