How to start an Amazon EC2 instance programmatically in .NET

Ben White picture Ben White · Sep 14, 2011 · Viewed 12.8k times · Source

I have been attempting to start an instance of EC2 in C# without luck.

When passing in an instance id to start the instance I get an error that the instance cannot be found despite that I am passing in an instance ID that I have obtained from the object property.

I would be most grateful for any tips or pointers with this.

Answer

Mehdi LAMRANI picture Mehdi LAMRANI · Mar 9, 2012

Amazon made huge efforts to integrate its AWS Cloud .Net SDK To VS2008 & VS 2010

  • 1 - Download and Install the AWS SDK msi
  • 2 - Create an AWS Console project, enter your credentials
    (available from your AWS Console under your login name menu on the top right corner)
  • 3 - Add the following code (see below images).
  • 4 - Your're done. It's very straightforward.
    You can check the programmatic start/stop success by refreshing your AWS Console Screen.

enter image description here

enter image description here

AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client();
//Start Your Instance
ec2.StartInstances(new StartInstancesRequest().WithInstanceId("i-00000000"));
//Stop it
ec2.StopInstances(new StopInstancesRequest().WithInstanceId("i-00000000"));

You just need to replace "i-00000000" by your instance Id (available in your AWS Management Console)

Hope this helps those googling this and stumbling upon this question (as I did myself) start off quickly.
Following these simple steps via these wizards will spare you considerable headaches.