def calculate_num_groups(conf, num_networks=None):
    """
    Calculates how many prefix groups we can have per AWS zone. Takes into
    account that we need a route for each prefix group and we can't have more
    than 48 route total.

    """
    num_zones  = len(conf['aws']['zones'])
    num_nets   = len(conf['networks']) if num_networks is None else \
                                       num_networks
    num_groups = 32

    while num_groups * num_zones * num_nets > 48:
        if num_groups == 1:
            raise Exception("Too many networks and/or zones, reaching "
                            "50 route limit for AWS.")
        num_groups //= 2
    return num_groups