我正在使用AWS,Python和Boto library。使用Boto轮询停止或启动EC2实例
我想在Boto EC2实例上调用.start()
或.stop()
,然后“轮询”它直到它完成。
import boto.ec2
credentials = {
'aws_access_key_id': 'yadayada',
'aws_secret_access_key': 'rigamarole',
}
def toggle_instance_state():
conn = boto.ec2.connect_to_region("us-east-1", **credentials)
reservations = conn.get_all_reservations()
instance = reservations[0].instances[0]
state = instance.state
if state == 'stopped':
instance.start()
elif state == 'running':
instance.stop()
state = instance.state
while state not in ('running', 'stopped'):
sleep(5)
state = instance.state
print " state:", state
然而,在最后while
循环,状态似乎得到“卡”在任“待定”或“停止”。强调“似乎”,从我的AWS控制台,我可以看到实际上确实使它“开始”或“停止”。
我能解决这个问题的唯一办法就是召回在while
循环.get_all_reservations()
,像这样:
while state not in ('running', 'stopped'):
sleep(5)
# added this line:
instance = conn.get_all_reservations()[0].instances[0]
state = instance.state
print " state:", state
是否有打电话让instance
将报告的实际状态的方法?
完美 - 工作恰到好处。不是没有,但我想说:我读了文档,我只是加倍检查...这种方法是不是在写这篇文章的文档!再次感谢。 – 2014-10-21 20:34:05
@garnaat请编辑你的答案并在boto3中添加boto3的指令,而不是'update()'你需要使用'load()' - 供将来的用户看 – bluesummers 2017-06-28 09:52:33