def _launch_ec2_instances()

in services/jenkins-autoscaling/lambda_mxnet_ci/autoscaling/handler.py [0:0]


def _launch_ec2_instances(scale_up_slots, ec2_resource):
    """
    Launch ec2 instances, matching the appropriate labels
    :param scale_up_slots:
    :param ec2_resource:
    :return: List of started instance names. Allows to determine whether some instances were not started.
    """
    jobs = []
    launch_templates = _launch_templates()

    # Start each instance one by one as each of them require different user data
    for label, target_instance_names in scale_up_slots.items():
        if label not in launch_templates:
            logging.error('No launch template for %s defined', label)
            continue

        launch_template = launch_templates[label]
        launch_template_id = launch_template['id']

        for target_instance_name in target_instance_names:
            logging.debug('Launching instance %s of type %s', target_instance_name, label)
            user_data_command = _format_ec2_user_data_command(label=label, target_instance_name=target_instance_name)
            if user_data_command is None:  # pragma: no cover
                logging.error('No user data command defined for %s, skipping...', target_instance_name)
                continue
            else:
                # Enqueue job
                jobs.append({
                    'label': label,
                    'target_instance_name': target_instance_name,
                    'launch_template_id': launch_template_id,
                    'user_data_command': user_data_command
                })

    started_instance_names = Parallel(n_jobs=min(AWS_PARALLEL_REQUESTS_LIMIT, len(jobs)), backend="threading")(
        delayed(_launch_ec2_instance)(ec2_resource=ec2_resource, label=job['label'],
                                      target_instance_name=job['target_instance_name'],
                                      launch_template_id=job['launch_template_id'],
                                      user_data_command=job['user_data_command']) for job in jobs)

    return [x for x in started_instance_names if x is not None]