from functools import reduce


def get_image_map(image_data, instance_type='instance-store'):
    """
    Build EC2 region to AMI map table

    :param image_data: The raw image data as returned from get_image_data
    :param instance_type: The instance type to include in the map. One of ['instance-store', 'ebs-ssd']
    :return: dictionary associating ec2 regions with corresponding ubuntu ami. I.e.
    { 'ap-northeast-1': {'ami': 'ami-71b81e10'} }
    """
    image_data = filter(lambda x: len(x.split()) > 7, image_data)
    image_map = [dict(zip(['instance_type', 'region', 'ami'],
                          [x.split()[4], x.split()[6], x.split()[7]]))
                 for x in image_data]
    image_map = [dict(zip(['region', 'ami'], [z['region'], z['ami']]))
                 for z in image_map if z['instance_type'] == instance_type]
    image_map = reduce(lambda x, y: {**x, **{y['region']: {'ami': y['ami']}}}, [{}] + image_map)
    return image_map