I'm in the process of writing code to list out the Instance IDs and Instance Types using the .net SDK for AWS. I know I need to use the DescribeInstancesRequest class. I also think I need to use an ArrayList to store the string outputs and a foreach loop to grab all of the instances. I'm just having trouble putting it together! Something as simple as listing the Instance IDs in the console would be a great help to me.
Has anyone done something similar?
UPDATE 10/13/2012
I'm making very tiny steps towards getting to where I need to be. I'm brand new to the .net AWS SDK so this is a very slow process. The code below counts the number of instances I have and returns it. Now I just need to figure out how to have it list the actual instance IDs.
Any ideas?
AmazonEC2 ec2 = new AmazonEC2Client();
DescribeInstancesRequest request = new DescribeInstancesRequest();
DescribeInstancesResponse res = ec2.DescribeInstances(request);
Console.WriteLine(res.DescribeInstancesResult.Reservation.Count);
Console.Read();
Not to beat a dead horse, but for my task, I had to print out the Instance IDs and Instance Types in a HTML table. This could be useful if you want to host it on a website. All you need to do is change the path of StreamWriter
. Here is my sloppy however finished product.
AmazonEC2 ec2 = new AmazonEC2Client();
DescribeInstancesRequest req = new DescribeInstancesRequest();
List<Amazon.EC2.Model.Reservation> result = ec2.DescribeInstances(req).DescribeInstancesResult.Reservation;
using (StreamWriter writer = new StreamWriter(@"C:\Users\Ethan\Desktop\InstanceOutput.html")) //change this output to a local address if testing the code
{
writer.Write("<table border=1><tbody><tr><th>Instance ID</th><th>Instance Type</th></tr>");
foreach (Amazon.EC2.Model.Reservation reservation in result)
{
foreach (Amazon.EC2.Model.RunningInstance runningInstance in reservation.RunningInstance)
{
writer.WriteLine("<tr><td>" + runningInstance.InstanceId + "</td><td>" + runningInstance.InstanceType + "</td></tr>");
}
}
writer.Write("</tbody></table>");
}
So keeping in mind I'm brand new to this, someone out there may be able to come up with a cleaner, more efficient, and better answer. This is how I got the instance ID and Instance Type to print in the console.
AmazonEC2 ec2 = new AmazonEC2Client();
DescribeInstancesRequest req = new DescribeInstancesRequest();
List<Amazon.EC2.Model.Reservation> result = ec2.DescribeInstances(req).DescribeInstancesResult.Reservation;
foreach (Amazon.EC2.Model.Reservation reservation in result)
{
foreach (Amazon.EC2.Model.RunningInstance runningInstance in reservation.RunningInstance)
{
Console.WriteLine(runningInstance.InstanceId);
Console.WriteLine(runningInstance.InstanceType);
}
}
something is obsoleted
List<Reservation> listReservations = ec2Client.DescribeInstances(requestInstances).Reservations;
foreach (Reservation reservation_item in listReservations)
{
foreach (Instance instance_item in reservation_item.Instances)
{
WriteLine(instance_item.InstanceId);
WriteLine(instance_item.InstanceType);
}
}