def _launch_ec2_instance()

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


def _launch_ec2_instance(ec2_resource, label, target_instance_name, launch_template_id, user_data_command):
    try:
        ec2_resource.meta.client.run_instances(
            DryRun=False,
            MaxCount=1,
            MinCount=1,
            LaunchTemplate={
                'LaunchTemplateId': launch_template_id,
                'Version': '$Default'
            },
            TagSpecifications=[
                {
                    'ResourceType': 'instance',
                    'Tags': [
                        {
                            'Key': 'Name',
                            'Value': target_instance_name
                        },
                        {
                            'Key': 'AutoScaledSlave',
                            'Value': 'True'
                        },
                        {
                            'Key': 'label',
                            'Value': label
                        }
                    ]
                },
            ],
            UserData=user_data_command
        )
        return target_instance_name
    except ClientError as client_error:
        error_code = client_error.response['Error']['Code']
        if error_code == 'InsufficientInstanceCapacity':
            logging.info("Insufficient instance capacity, can't launch %s: %s",
                         target_instance_name, client_error.response['Error']['Message'])
        else:
            logging.exception('Exception occurred during instance launch')

    # Make sure to catch everything, otherwise we won't be able to clean up properly
    except Exception:  # pylint: disable=broad-except
        logging.exception('Unexpected exception during instance launch')