Getting state of EC2 instance Java API

Question:

I am trying to automate some stuff on AWS and I am using Java API to do this. I am able to launch number of instances I want, but I am unable to find the right class or method that can fetch me the current state of the instance (like running, stopped etc).

I tried this

DescribeInstanceStatusRequest describeInstance = new DescribeInstanceStatusRequest().withInstanceIds(instances.get(i).getInstanceId());
DescribeInstanceStatusResult attr = ec2.describeInstanceStatus(describeInstance);
List<InstanceStatus> state = attr.getInstanceStatuses();

not sure why the instance status is a list

Can someone please point me to right API call.

Thanks in advance

Answer:

I figured the issue. working code

Leave a Reply