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

Dam*_*men 1 amazon-ec2 amazon-web-services boto3

我开始EC2 Instances使用以下代码

def start_ec2_instances(self, instanceids):
    ec2client = boto3.resource('ec2')
    response = ec2client.start_instances(InstanceIds=instanceids)
    return
Run Code Online (Sandbox Code Playgroud)

现在它成功启动.但是,我想使用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
Run Code Online (Sandbox Code Playgroud)

hel*_*loV 5

使用

试试这个:

ec2 = boto3.client('ec2')
start_ec2_instances(instanceids)
waiter = ec2.get_waiter('instance_running')
waiter.wait(InstanceIds=instanceids)
Run Code Online (Sandbox Code Playgroud)

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