我正在使用.net SDK for AWS编写代码以列出实例ID和实例类型。我知道我需要使用DescribeInstancesRequest类。我还认为我需要使用ArrayList来存储字符串输出,并使用foreach循环来获取所有实例。我只是把它放在一起很麻烦!像在控制台中列出实例ID这样简单的事情对我来说是一个很大的帮助。
有没有人做过类似的事情?
我正迈出一小步才能到达我需要的地方。我是.net AWS SDK的新手,所以这是一个非常缓慢的过程。下面的代码计算我拥有的实例数并返回它。现在我只需要弄清楚如何列出实际的实例ID。
有什么想法吗?
AmazonEC2 ec2 = new AmazonEC2Client();
DescribeInstancesRequest request = new DescribeInstancesRequest();
DescribeInstancesResponse res = ec2.DescribeInstances(request);
Console.WriteLine(res.DescribeInstancesResult.Reservation.Count);
Console.Read();
答案 0 :(得分:6)
不要打败死马,但对于我的任务,我必须在HTML表格中打印出实例ID和实例类型。如果您想在网站上托管它,这可能很有用。您需要做的就是更改StreamWriter
的路径。这是我的草率但成品。
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>");
}
所以请记住,我对此不熟悉,有人可能会想出一个更清洁,更有效,更好的答案。这就是我在控制台中打印实例ID和实例类型的方法。
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);
}
}
答案 1 :(得分:-1)
某些东西已过时
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);
}
}