Click here to Skip to main content
13,864,291 members
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 12 Aug 2010
Licenced CPOL

Easy Amazon EC2

, 30 Jun 2016
Rate this:
Please Sign up or sign in to vote.
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.


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:

// Initialize the service
AmazonEC2 service = new AmazonEC2Client(AWSAccessKey, AWSSecretAccessKey);
// Validate the service with a basic EC2 query
DescribeImagesRequest request = new DescribeImagesRequest();

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.

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...

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 = 

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:

Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.ContentType = "Content-Type=application/x-rdp rdp;charset=ISO-8859-1";
Response.AddHeader("Content-Length", content.Length.ToString());

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,, 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.


  • 12 Aug 2010: Initial release


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


About the Author

Simon S Ellis
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
GregoryW16-Jul-13 2:53
memberGregoryW16-Jul-13 2:53 
QuestionHow to detect the machine is reachable already? Pin
Dioksin29-Mar-13 1:20
memberDioksin29-Mar-13 1:20 
GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 9:28
memberKanasz Robert27-Sep-12 9:28 
QuestionNice article! Pin
Jay Macro7-Sep-12 12:46
memberJay Macro7-Sep-12 12:46 
GeneralMy vote of 5 Pin
Abinash Bishoyi1-Aug-12 2:55
memberAbinash Bishoyi1-Aug-12 2:55 
GeneralExcellent, super work. and one question Pin
imak19-Aug-10 6:49
memberimak19-Aug-10 6:49 
GeneralRe: Excellent, super work. and one question Pin
Simon S Ellis19-Aug-10 10:37
memberSimon S Ellis19-Aug-10 10:37 
GeneralMy vote of 5 Pin
Member 724777613-Aug-10 13:01
memberMember 724777613-Aug-10 13:01 
GeneralMy vote 5 Pin
redjes12-Aug-10 21:43
memberredjes12-Aug-10 21:43 
GeneralMy vote of 5 Pin
defwebserver12-Aug-10 8:03
memberdefwebserver12-Aug-10 8: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 1 Jul 2016
Article Copyright 2010 by Simon S Ellis
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid