我試圖通過aws java api以編程方式列出我的ec2實例。AWS/Java:如何從AWS Java API獲取EC2實例
public static void main(String[] args) {
AWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
AmazonEC2AsyncClient amazonEC2AsyncClient = new AmazonEC2AsyncClient(awsCredentials);
DescribeAvailabilityZonesRequest describeAvailabilityZonesRequest = new DescribeAvailabilityZonesRequest();
Future<DescribeAvailabilityZonesResult> describeAvailabilityZonesResultFuture = amazonEC2AsyncClient.describeAvailabilityZonesAsync(describeAvailabilityZonesRequest);
try {
DescribeAvailabilityZonesResult describeAvailabilityZonesResult = describeAvailabilityZonesResultFuture.get();
System.out.println(describeAvailabilityZonesResult);
} catch (Exception e) {
e.printStackTrace();
}
}
這將打印類似:
{AvailabilityZones: [
{ZoneName: us-east-1a,State: available,RegionName: us-east-1,Messages: []},
{ZoneName: us-east-1b,State: available,RegionName: us-east-1,Messages: []},
{ZoneName: us-east-1c,State: available,RegionName: us-east-1,Messages: []}]}
但是,我知道我有例如在美國中西部-2。當我通過
describeAvailabilityZonesRequest.withZoneNames("us-west-2");
添加到我的API查詢我得到這個錯誤
java.util.concurrent.ExecutionException: com.amazonaws.AmazonServiceException: Status Code: 400, AWS Service: AmazonEC2, AWS Request ID: 38956486-2686-4466-b9e0-45ef12395676, AWS Error Code: InvalidParameterValue,
AWS Error Message: Invalid availability zone: [us-west-2]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:188)
at com.pledgeling.platform.system.SystemAdmin.main(SystemAdmin.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: com.amazonaws.AmazonServiceException: Status Code: 400, AWS Service: AmazonEC2, AWS Request ID: 38956486-2686-4466-b9e0-45ef12395676, AWS Error Code: InvalidParameterValue, AWS Error Message: Invalid availability zone: [us-west-2]
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:767)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:414)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:228)
at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:7918)
我應該怎麼做來解決這個問題?爲什麼我的西方實例沒有顯示出來?
Michael,非常感謝您的回答和鏈接。完美清晰,正確。 –
我做到了這一點,但它仍然只顯示我們東1 – user3217883
EndpointConfiguration endpointConfiguration = new EndpointConfiguration(「ec2.us-west-2.amazonaws.com」,「us-west-2」); \t \t EC2 = AmazonEC2ClientBuilder.standard() \t \t \t \t .withEndpointConfiguration(endpointConfiguration) .build(); – user3217883