如何使用API​​从AWS账户获取列表保留实例(预先保留)

时间:2010-12-01 09:25:21

标签: amazon-ec2 amazon-web-services

我正在尝试构建一个报告,其中列出了预先保留一次预付费的特定帐户的所有实例。

1 个答案:

答案 0 :(得分:0)

此C#代码将为您提供您拥有的保留实例列表:

AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client(
    accessKey,
    secretAccessKey
    );

DescribeReservedInstancesRequest request = new DescribeReservedInstancesRequest();

try
{
    DescribeReservedInstancesResponse response = ec2.DescribeReservedInstances(request);

    foreach (ReservedInstances ri in response.DescribeReservedInstancesResult.ReservedInstances)
    {
        Console.WriteLine(ri.ReservedInstancesId + "\t" + ri.InstanceType + "\t" + ri.InstanceCount.ToString() + "\t" + ri.PurchaseState);
    }
}
catch (AmazonEC2Exception ex)
{
    Console.WriteLine("Caught Exception: " + ex.Message);
    Console.WriteLine("Response Status Code: " + ex.StatusCode);
    Console.WriteLine("Error Code: " + ex.ErrorCode);
    Console.WriteLine("Error Type: " + ex.ErrorType);
    Console.WriteLine("Request ID: " + ex.RequestId);
    Console.WriteLine("XML: " + ex.XML);
}