def create_instance(args):
    client = get_client(args.region)
    vars = {'ImageId': args.machine_image, 'KeyName': args.key_pair_name, 'MinCount': 1, 'MaxCount': 1, 'InstanceType': args.instance_type}
    sg_ids = (args.security_group_id.split(',') if args.security_group_id else None)
    if (sg_ids is None):
        vpc = get_vpc_for_subnet(client, args.cloud_subnet)
        sg_name = get_yb_sg_name(args.region)
        sg = get_security_group(client, sg_name, vpc)
        sg_ids = [sg.id]
    vars['NetworkInterfaces'] = [{'DeviceIndex': 0, 'AssociatePublicIpAddress': args.assign_public_ip, 'SubnetId': args.cloud_subnet, 'Groups': sg_ids}]
    volumes = []
    ebs = {'DeleteOnTermination': True, 'VolumeSize': 40, 'VolumeType': 'gp2'}
    if (args.cmk_res_name is not None):
        ebs['Encrypted'] = True
        ebs['KmsKeyId'] = args.cmk_res_name
    volumes.append({'DeviceName': '/dev/sda1', 'Ebs': ebs})
    device_names = get_device_names(args.instance_type, args.num_volumes)
    for (i, device_name) in enumerate(device_names):
        volume = {}
        if has_ephemerals(args.instance_type):
            volume = {'DeviceName': '/dev/{}'.format(device_name), 'VirtualName': 'ephemeral{}'.format(i)}
        elif is_next_gen(args.instance_type):
            ebs = {'DeleteOnTermination': True, 'VolumeType': args.volume_type, 'VolumeSize': args.volume_size}
            if (args.cmk_res_name is not None):
                ebs['Encrypted'] = True
                ebs['KmsKeyId'] = args.cmk_res_name
            if (args.volume_type == 'io1'):
                ebs['Iops'] = args.disk_iops
            volume = {'DeviceName': '/dev/{}'.format(device_name), 'Ebs': ebs}
        volumes.append(volume)
    vars['BlockDeviceMappings'] = volumes

    def __create_tag(k, v):
        return {'Key': k, 'Value': v}
    instance_tags = [__create_tag('Name', args.search_pattern), __create_tag('launched-by', os.environ.get('USER', 'unknown')), __create_tag('yb-server-type', args.type)]
    custom_tags = (args.instance_tags if (args.instance_tags is not None) else '{}')
    for (k, v) in json.loads(custom_tags).iteritems():
        instance_tags.append(__create_tag(k, v))
    vars['TagSpecifications'] = [{'ResourceType': 'instance', 'Tags': instance_tags}]
    instance_ids = client.create_instances(**vars)
    if (len(instance_ids) != 1):
        logging.error('Invalid create_instances response: {}'.format(instance_ids))
        raise YBOpsRuntimeError('Expected to create 1 instance, got {}'.format(len(instance_ids)))
    instance = instance_ids[0]
    instance.wait_until_running()
def create_instance(args):
    client = get_client(args.region)
    vars = {'ImageId': args.machine_image, 'KeyName': args.key_pair_name, 'MinCount': 1, 'MaxCount': 1, 'InstanceType': args.instance_type}
    sg_ids = (args.security_group_id.split(',') if args.security_group_id else None)
    if (sg_ids is None):
        vpc = get_vpc_for_subnet(client, args.cloud_subnet)
        sg_name = get_yb_sg_name(args.region)
        sg = get_security_group(client, sg_name, vpc)
        sg_ids = [sg.id]
    vars['NetworkInterfaces'] = [{'DeviceIndex': 0, 'AssociatePublicIpAddress': args.assign_public_ip, 'SubnetId': args.cloud_subnet, 'Groups': sg_ids}]
    volumes = []
    ebs = {'DeleteOnTermination': True, 'VolumeSize': 40, 'VolumeType': 'gp2'}
    if (args.cmk_res_name is not None):
        ebs['Encrypted'] = True
        ebs['KmsKeyId'] = args.cmk_res_name
    volumes.append({'DeviceName': '/dev/sda1', 'Ebs': ebs})
    device_names = get_device_names(args.instance_type, args.num_volumes)
    for (i, device_name) in enumerate(device_names):
        volume = {}
        if has_ephemerals(args.instance_type):
            volume = {'DeviceName': '/dev/{}'.format(device_name), 'VirtualName': 'ephemeral{}'.format(i)}
        elif is_next_gen(args.instance_type):
            ebs = {'DeleteOnTermination': True, 'VolumeType': args.volume_type, 'VolumeSize': args.volume_size}
            if (args.cmk_res_name is not None):
                ebs['Encrypted'] = True
                ebs['KmsKeyId'] = args.cmk_res_name
            if (args.volume_type == 'io1'):
                ebs['Iops'] = args.disk_iops
            volume = {'DeviceName': '/dev/{}'.format(device_name), 'Ebs': ebs}
        volumes.append(volume)
    vars['BlockDeviceMappings'] = volumes

    def __create_tag(k, v):
        return {'Key': k, 'Value': v}
    instance_tags = [__create_tag('Name', args.search_pattern), __create_tag('launched-by', os.environ.get('USER', 'unknown')), __create_tag('yb-server-type', args.type)]
    custom_tags = (args.instance_tags if (args.instance_tags is not None) else '{}')
    for (k, v) in json.loads(custom_tags).iteritems():
        instance_tags.append(__create_tag(k, v))
    vars['TagSpecifications'] = [{'ResourceType': 'instance', 'Tags': instance_tags}]
    instance_ids = client.create_instances(**vars)
    if (len(instance_ids) != 1):
        logging.error('Invalid create_instances response: {}'.format(instance_ids))
        raise YBOpsRuntimeError('Expected to create 1 instance, got {}'.format(len(instance_ids)))
    instance = instance_ids[0]
    instance.wait_until_running()