我尝试使用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")
答案 0 :(得分:1)
感谢@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)
答案 1 :(得分:0)
我尝试过instance.wait_until_running()。将实例更新为运行状态需要一些时间。根据amazon docs link,它表示实例至少需要60秒才能启动。这是一个适合我的示例代码。希望它有所帮助!
;创建5个实例
ec2.create_instances(ImageId =&#39;&#39;,MinCount = 1,MaxCount = 5) time.sleep(60)
;打印您的实例