def _match_reserved_instances(reserved_groups, itype, in_vpc, zone, tenancy):
    """Try to match reserved instance in reserved_groups, if it does,
    remove the instance from reserved_groups and return the reserved instance.
    If no reserved instance matches, return None

    """
    for key in [
        # match the same VPC instances first
        (itype, in_vpc, zone, tenancy),
        # since VPC doesn't really affect the billing, so we also try to
        # match the oppsite VPC setting instances too
        (itype, not in_vpc, zone, tenancy),
    ]:
        reserved_instances = reserved_groups.get(tuple(key))
        if reserved_instances:
            ri = reserved_instances.pop()
            return ri