2012-01-12 109 views
6

我想在AWS上自動化一些內容,並且正在使用Java API來執行此操作。我能夠啓動一些我想要的實例,但是我無法找到能夠獲取實例當前狀態的正確類或方法(如運行,停止等)。獲取EC2實例Java API的狀態

我想這

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

不知道爲什麼實例狀態是一個列表

可有人請點我正確的API調用。

在此先感謝

回答

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

現在我該如何等到Tomcat在該EC2實例上啓動? 。 – 2014-01-16 13:37:08

+1

如果你想獲得的所有實例的狀態,你可以做'DescribeInstanceStatusRequest請求=新DescribeInstanceStatusRequest()withIncludeAllInstances(真) \t \t \t \t .withInstanceIds(instanceIds);' – 2016-10-05 10:18:20

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)返回所有機器的狀態。

+0

這是一個具體的使用情況下,我知道這是重新啓動或開始,而不是停止。 此問題剛剛發佈,我不確定AWS API是否已更改。 – tazo 2014-01-25 18:15:56