How can I get the count of server instance开发者_Go百科s running under my Amazon EC2 account using API
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
精彩评论