12

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

4

1 回答 1

17

我通过使用过滤器来做到这一点,例如获取由相同密钥对值创建的所有实例

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


    }
}
于 2012-08-28T11:58:28.767 回答