I'm looking for some code that will return the current instance state regardless of whether the instance is currently running or not. I found some code that will return the desired result on a running instance, but when trying to find one that will work on stopped instances, I was overwhelmed by a number of similar looking classes that appeared to perform a similar operation, but in the end, did not work or were poorly documented.
Anyway, the running instance version of the code is below:
public Integer getInstanceStatus(String instanceId) {
DescribeInstanceStatusRequest describeInstanceRequest = new DescribeInstanceStatusRequest().withInstanceIds(instanceId);
DescribeInstanceStatusResult describeInstanceResult = ec2.describeInstanceStatus(describeInstanceRequest);
List<InstanceStatus> state = describeInstanceResult.getInstanceStatuses();
return state.get(0).getInstanceState().getCode();
}
So I'm basically looking for the equivalent that does not have the DescribeInstanceStatus
's restriction that requires the instance to be running. I'd assume this is possible since the getCode()
documentation shows it as being able to return the value 80 which denotes a stopped instance.