我想基于我存储的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为您提供了一种访问实例状态并能够监视任何更改的方法.但是,一个全新的实例的状态怎么样?
dnl*_*rky 10
我没有在文档中看到这一点,但以下内容对我有用:
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.
等待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)
通过这种机制,您将能够轮询何时出现新实例。
| 归档时间: |
|
| 查看次数: |
14646 次 |
| 最近记录: |