Reputation: 1125
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
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
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