1

我正在尝试使用以下代码描述正在使用的运行 ec2 实例:

DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
            List<Reservation> reservations = describeInstancesRequest.getReservations();
            Set<Instance> instances = new HashSet<Instance>();

            for (Reservation reservation : reservations) {
                instances.addAll(reservation.getInstances());
            }

            System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");

我得到的返回结果为 0。我猜这是因为默认 AZ 设置为美国东部。

我的实例在美国西部运行。

如何更改 AZ?

4

2 回答 2

1

您可以像这样设置端点:

ec2.setEndpoint("ec2.us-west-1.amazonaws.com");

于 2012-11-16T21:47:45.460 回答
1

对于那些遇到这篇文章的人来说,目前的做法是:

    AmazonEC2 ec2 = new AmazonEC2Client(new StaticCredentialsProvider(new BasicAWSCredentials("your-aws-access-key-here", "your-aws-secret-here")));
    DescribeInstancesRequest request = new DescribeInstancesRequest();

    Filter filter = new Filter()
            .withName("availability-zone")
            .withValues("us-east-1a");

    request.withFilters(filter);

    DescribeInstancesResult result = ec2.describeInstances(request);
    List<Reservation> reservations = result.getReservations();
于 2015-09-22T19:34:18.103 回答