简体   繁体   English

AWS / Java:如何从AWS Java API获取EC2实例

[英]AWS/Java: How to Get EC2 Instances From AWS Java API

I am trying to programmatically list my ec2 instances via the aws java api. 我试图通过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();
    }
}

This prints something like: 打印如下:

{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: []}]}

However, I know I have instance in us-west-2. 但是,我知道在us-west-2中有实例。 When I add that to my api query via 当我通过将其添加到我的api查询中时

describeAvailabilityZonesRequest.withZoneNames("us-west-2");

I get this error 我得到这个错误

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)

What should I do to fix this? 我应该怎么做才能解决这个问题? Why are my us-west instances not showing up? 为什么我的美国西部实例没有显示?

us-west-2 is not an availability zone, it's a region. us-west-2不是可用区,而是一个区域。 us-west-2a is an availability zone. us-west-2a是一个可用区。 It looks like you're getting the error because, as your code is currently written, you're asking us-east-1 about an availability zone that doesn't exist there. 您似乎正在收到错误消息,因为在您当前编写代码时,您正在向us-east-1询问那里不存在的可用区。 The fix, though, is not just a matter of putting the final letter designator in the string. 但是,解决方法不仅仅是将最后一个字母标记放入字符串中。

EC2 is deliberately decentralized so that each region is independent of the others for survivability, and as such, each region has its own API endpoint. EC2有意分散,因此每个区域在生存性上都与其他区域无关,因此,每个区域都有自己的API端点。 Your requests have to be sent to the appropriate endpoint for the region you want. 您的请求必须发送到所需区域的适当端点。

I'm not a Java person, so I'm not sure if the following is precisely the information you need, but it should at least get you pointed in the right direction. 我不是Java人士,所以我不确定以下内容是否正是您所需要的信息,但至少应该使您有正确的方向。

Each AWS client can be configured to use a specific endpoint by calling the setEndpoint(String endpointUrl) method. 通过调用setEndpoint(String endpointUrl)方法,可以将每个AWS客户端配置为使用特定的终端setEndpoint(String endpointUrl)

http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/java-dg-region-selection.html http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/java-dg-region-selection.html

The link above also explains why you're currently talking to us-east-1 without actually specifying an endpoint. 上面的链接还说明了为什么您当前在不实际指定端点的情况下与us-east-1交谈。

The AWS SDK for Java uses the US East (Northern Virginia) Region as the default region if you do not specify a region in your code. 如果您未在代码中指定区域,则适用于Java的AWS开发工具包会将美国东部(弗吉尼亚北部)区域用作默认区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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