Vitaly
Vitaly

Reputation: 2662

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?

Upvotes: 1

Views: 724

Answers (1)

phoenix
phoenix

Reputation: 3159

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.

Upvotes: 1

Related Questions