EC2:等待新实例处于运行状态

Dio*_*Dio 10 amazon-ec2

我想基于我存储的AMI创建一个新实例.

我通过以下代码实现此目的:

RunInstancesRequest rir = new RunInstancesRequest(imageId,1, 1);
// Code for configuring the settings of the new instance
...
RunInstancesResult runResult = ec2.runInstances(rir);
Run Code Online (Sandbox Code Playgroud)

但是,我找不到等待"阻塞"/等待,直到实例启动并运行Thread.currentThread().sleep(xxxx)命令.

另一方面,StartInstancesResult和TerminateInstancesResult为您提供了一种访问实例状态并能够监视任何更改的方法.但是,一个全新的实例的状态怎么样?

Zag*_*ags 23

boto3有:

instance.wait_until_running()
Run Code Online (Sandbox Code Playgroud)


dnl*_*rky 10

适用于v1.6.0AWS CLI更新日志:

添加一个wait子命令,允许命令阻止,直到AWS资源达到给定状态(issue 992,issue 985)

我没有在文档中看到这一点,但以下内容对我有用:

aws ec2 start-instances --instance-ids "i-XXXXXXXX"
aws ec2 wait instance-running --instance-ids "i-XXXXXXXX"
Run Code Online (Sandbox Code Playgroud)

wait instance-running在EC2实例运行之前,该线路没有完成.

我不使用Python/boto/botocore,但假设它有类似的东西.查看Github上的waiter.py.


j0n*_*nes 5

等待EC2实例准备就绪是一种常见的模式。在Python库boto中,您还可以通过sleep调用解决此问题:

   reservation = conn.run_instances([Instance configuration here])
   instance = reservation.instances[0]

   while instance.state != 'running':
       print '...instance is %s' % instance.state
       time.sleep(10)
       instance.update()
Run Code Online (Sandbox Code Playgroud)

通过这种机制,您将能够轮询何时出现新实例。