简体   繁体   中英

How to get AWS EC2 Instance object by id from java code?

I hava AmazonEC2Client connected to my account. I can ask AWS my Reservations and Instances by

DescribeInstancesResult describeInstancesRequest = amazonEC2Client.describeInstances();
List<Reservation> reservations = describeInstancesRequest.getReservations();
Set<Instance> instances = new HashSet<Instance>();

for (Reservation reservation : reservations) {
   instances.addAll(reservation.getInstances());
}

But if I already know the Id of instance, can I somehow get Instance object for it without requesting reservations and all instances?

You need to use :
describeInstances (DescribeInstancesRequest describeInstancesRequest)
instead of
describeInstances() .

You can set the instanceId in the DescribeInstancesRequest .
You need to use the setInstanceIds(Collection<String> instanceIds) method of DescribeInstancesRequest to set the instanceId(s) you are searching for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM