我想使用java aws sdk获取在我的AWS账户下运行的特定标签的所有实例ID。有人可以指导我,我怎么能得到这个。谢谢
答案 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());
}
}