waqas
waqas

Reputation: 1125

Check whether ec2 instance is running or terminated using java aws sdk

How can we check whether the instance is running or terminated? As terminated instance id remains in the system for some time, but I want to exclude those terminated instances from my running instances list as soon as the instance is terminated. Can someone please guide me how can I achieve it?

Upvotes: 2

Views: 4305

Answers (2)

Ajay Tiwari
Ajay Tiwari

Reputation: 3448

You can call following instruction to do so.

List<Reservation> reservList = ec2.describeInstances().getReservations(); 

//iterate on reservList and call 

List<Instance> instanceList =  reservList[i].getInstances();

//Now on each instance you can call 

 instanceList[i].getState().getName();

This will return the state of each of your instance

Upvotes: 2

waqas
waqas

Reputation: 1125

To get rid of terminated or stoped instances, I just put on a check like this

 if(reservation.getInstances().getPublicIpAddress()!= null)

Upvotes: 2

Related Questions