如何在AWS Java上为访问的EC2对象设置可用区域

时间:2012-11-16 21:42:28

标签: java amazon-ec2 amazon-web-services availability-zone

我正在尝试使用以下代码描述正在运行的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设置为US-East。

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

如何更改AZ?

2 个答案:

答案 0 :(得分:1)

您可以像这样设置端点:

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

答案 1 :(得分: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();