如何使用aws java sdk在我的amazon帐户下获取带有标记的所有实例

时间:2012-08-28 10:13:12

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

我想使用java aws sdk获取在我的AWS账户下运行的特定标签的所有实例ID。有人可以指导我,我怎么能得到这个。谢谢

1 个答案:

答案 0 :(得分:16)

我是通过使用过滤器来完成的,例如获取由相同的键对值

创建的所有实例
DescribeInstancesRequest request = new DescribeInstancesRequest();
List<String> valuesT1 = new ArrayList<String>();
valuesT1.add("my-keypair-name");
Filter filter = new Filter("key-name", valuesT1);

DescribeInstancesResult result = ec2.describeInstances(request.withFilters(filter));

List<Reservation> reservations = result.getReservations();

for (Reservation reservation : reservations) {
    List<Instance> instances = reservation.getInstances();

    for (Instance instance : instances) {

        System.out.println(instance.getInstanceId());


    }
}