获取EC2实例Java API的状态

时间:2012-01-12 01:18:35

标签: amazon-ec2 amazon-web-services

我正在尝试在AWS上自动化一些东西,我正在使用Java API来执行此操作。我能够启动我想要的实例数,但是我无法找到能够获取实例当前状态的正确类或方法(如运行,停止等)。

我试过这个

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

不确定为什么实例状态是列表

有人可以指出我正确的API电话。

提前致谢

2 个答案:

答案 0 :(得分:6)

我想到了这个问题。工作代码

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();

答案 1 :(得分:3)

我刚看这个问题

来自文档(http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstanceStatus.html),

  

DescribeInstanceStatus仅返回处于运行状态的实例的信息。

因此,如果机器停止而不是重新启动,您可能会永远等待。

DescribeInstances(http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html)返回所有计算机的状态。