gpt4 book ai didi

amazon-web-services - 使用 boto3 启动多个实例并等待它们完全启动

转载 作者:行者123 更新时间:2023-12-05 08:41:50 27 4
gpt4 key购买 nike

我正在使用以下代码启动一堆 EC2 Instances

def start_ec2_instances(self, instanceids):
ec2client = boto3.resource('ec2')
response = ec2client.start_instances(InstanceIds=instanceids)
return

现在启动成功了。但是我想使用 wait_until_running 方法来检查实例的状态并等待所有实例启动。

wait_until_running 方法只能在单个实例上发出吗?如何等待已使用 boto3

启动的实例列表

这就是我目前正在做的事情。但是想知道是否还有其他方法可以一次性完成

def wait_until_instance_running(self, instanceids):
ec2 = boto3.resource('ec2')
for instanceid in instanceids:
instance = ec2.Instance(instanceid)
logger.info("Check the state of instance: %s",instanceid)
instance.wait_until_running()
return

最佳答案

使用

试试这个:

ec2 = boto3.client('ec2')
start_ec2_instances(instanceids)
waiter = ec2.get_waiter('instance_running')
waiter.wait(InstanceIds=instanceids)

服务员函数每 15 秒轮询一次,直到达到成功状态。 40 次检查失败后返回错误。

关于amazon-web-services - 使用 boto3 启动多个实例并等待它们完全启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47225073/

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