如何使用API​​获取在我的Amazon EC2帐户下运行的服务器实例的数量

时间:2010-11-24 09:20:04

标签: amazon-ec2 amazon-web-services

如何使用API​​

获取在我的Amazon EC2帐户下运行的服务器实例的数量

2 个答案:

答案 0 :(得分:3)

以下是AWS .NET SDK附带的示例中的示例:

static void Main(string[] args)
{
    NameValueCollection appConfig = ConfigurationManager.AppSettings;

    AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client(
        appConfig["AWSAccessKey"],
        appConfig["AWSSecretKey"]
        );

    DescribeInstancesRequest request = new DescribeInstancesRequest();

    try
    {
        DescribeInstancesResponse ec2Response = ec2.DescribeInstances(request);
        int numInstances = 0;
        numInstances = ec2Response.DescribeInstancesResult.Reservation.Count;
        Console.WriteLine("You have " + numInstances + " Amazon EC2 instance(s) running.");
    }
    catch (AmazonEC2Exception ex)
    {
        if (ex.ErrorCode.Equals("OptInRequired"))
        {
            Console.WriteLine("You are not signed for Amazon EC2.");
            Console.WriteLine("You can sign up at http://aws.amazon.com/ec2.");
        }
        else
        {
            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);
        }
    }
    Console.WriteLine();
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey(true);
}

答案 1 :(得分:1)

“入门”页面在页面中间有一个示例

http://aws.amazon.com/articles/3586

下载java SDK并查看aws-java-sdk / samples / AwsConsoleApp / AwsConsoleApp.java