简体   繁体   中英

How can I get the count of server instances running under my Amazon EC2 account using API

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

Here is an example as seen in the samples included with the 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);
}

The Getting Started page has an example halfway down the page

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

download the java SDK and take a look at aws-java-sdk/samples/AwsConsoleApp/AwsConsoleApp.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM