2012-11-16 59 views
1

我試圖來形容運行,我使用下面的代碼EC2實例:如何在AWS的Java設置可用性區域爲訪問對象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?

回答

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(); 
1

您可以設置這樣的端點:

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