def start_instance()

in cdk/ide/lambda_functions/c9DiskResize/lambda_function.py [0:0]


def start_instance(instance_id):
    logger.debug("Start instance %s" % (instance_id))
    retry=0
    while True:
        logger.debug("Test %s stopped instamce" % (retry))
        instance = ec2_client.describe_instances(Filters=[{'Name': 'instance-id', 'Values': [instance_id]}])['Reservations'][0]['Instances'][0]
        if instance['State']['Name'] in ['stopped']:
            try:
                logger.debug("Instance %s stopped, start it.." % (instance_id))
                ec2_client.start_instances(InstanceIds=[instance_id, ])
                break
            except:
                logger.debug("Failed to start instance", exc_info=True)
                return
        else:
            retry += 1
            if retry >= 20:
                logger.debug("Too many attempts to restart instance %s" % (instance_id))
                break
            sleep(15)

    retry=0
    while True:
        logger.debug("Test %s running instamce" % (retry))
        instance = ec2_client.describe_instances(Filters=[{'Name': 'instance-id', 'Values': [instance_id]}])['Reservations'][0]['Instances'][0]
        if instance['State']['Name'] in ['running']:
            try:
                logger.debug("Instance %s running.." % (instance_id))
                break
            except:
                logger.debug("Failed to detect instance status", exc_info=True)
                return
        else:
            retry += 1
            if retry >= 20:
                logger.debug("Too many attempts to detect instance status %s" % (instance_id))
                break
            sleep(15)