Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting state of EC2 instance Java API

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

like image 472
pbathala Avatar asked Jan 12 '12 01:01

pbathala


1 Answers

I figured the issue. working code

DescribeInstanceStatusRequest describeInstanceRequest = new DescribeInstanceStatusRequest().withInstanceIds(i-xxxxxxxx);
DescribeInstanceStatusResult describeInstanceResult = ec2.describeInstanceStatus(describeInstanceRequest);
List<InstanceStatus> state = describeInstanceResult.getInstanceStatuses();
while (state.size() < 1) { 
    // Do nothing, just wait, have thread sleep if needed
    describeInstanceResult = ec2.describeInstanceStatus(describeInstanceRequest);
    state = describeInstanceResult.getInstanceStatuses();
}
String status = state.get(0).getInstanceState().getName();
like image 173
pbathala Avatar answered Sep 19 '22 00:09

pbathala