gpt4 book ai didi

python - boto3 wait_until_running 不能按预期工作

转载 作者:行者123 更新时间:2023-12-05 01:18:13 27 4
gpt4 key购买 nike

我正在尝试使用 boto3 编写脚本来启动实例并等待它启动。根据 wait_until_running 的文档,它应该等到实例完全启动(我假设检查应该没问题)但不幸的是它只适用于 wait_until_stopped 和 wait_until_running 它只是启动实例并且不会等到它是完全开始了。不确定我是否在这里做错了什么,或者这是 boto3 的错误。

代码如下:

import boto3


ec2 = boto3.resource('ec2',region_name="ap-southeast-2")
ec2_id = 'i-xxxxxxxx'
instance = ec2.Instance(id=ec2_id)
print("starting instance " + ec2_id)
instance.start()
instance.wait_until_running()
print("instance started")

最佳答案

感谢@Mark B @Madhurya Gandi这是适用于我的案例的解决方案:

import boto3,socket
retries = 10
retry_delay=10
retry_count = 0
ec2 = boto3.resource('ec2',region_name="ap-southeast-2")
ec2_id = 'i-xxxxxxxx'
instance = ec2.Instance(id=ec2_id)
print("starting instance " + ec2_id)
instance.start()
instance.wait_until_running()
while retry_count <= retries:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((instance.public_ip_address,22))
if result == 0:
Print "Instance is UP & accessible on port 22, the IP address is: ",instance.public_ip_address
break
else:
print "instance is still down retrying . . . "
time.sleep(retry_delay)
except ClientError as e:
print('Error', e)

关于python - boto3 wait_until_running 不能按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46379043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com