简体   繁体   中英

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?

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

为了摆脱终止或停止的实例,我只是进行了这样的检查

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

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