# Buggy code
def ensure_final_subnet(conn, module, subnet, start_time):
    for rewait in range(0, 10):
        map_public_correct = False
        assign_ipv6_correct = False

        if module.params["map_public"] == subnet["map_public_ip_on_launch"]:
            map_public_correct = True
        else:
            if module.params["map_public"]:
                handle_waiter(
                    conn,
                    module,
                    "subnet_has_map_public",
                    {"SubnetIds": [subnet["id"]]},
                    start_time,
                )
            else:
                handle_waiter(
                    conn,
                    module,
                    "subnet_no_map_public",
                    {"SubnetIds": [subnet["id"]]},
                    start_time,
                )

        if module.params["assign_instances_ipv6"] == subnet.get(
            "assign_ipv6_address_on_creation"
        ):
            assign_ipv6_correct = True
        else:
            if module.params["assign_instances_ipv6"]:
                handle_waiter(
                    conn,
                    module,
                    "subnet_has_assign_ipv6",
                    {"SubnetIds": [subnet["id"]]},
                    start_time,
                )
            else:
                handle_waiter(
                    conn,
                    module,
                    "subnet_no_assign_ipv6",
                    {"SubnetIds": [subnet["id"]]},
                    start_time,
                )

        if map_public_correct and assign_ipv6_correct:
            break

        time.sleep(3)
        subnet = get_matching_subnet(
            conn, module, module.params["vpc_id"], module.params["cidr"]
        )

    return subnet


# Traceback (ClientError)
Traceback (most recent call last):
File "/tmp/ansible_dPE6PL/ansible_module_ec2_vpc_net.py", line 203, in get_vpc
classic_link = connection.describe_vpc_classic_link(VpcIds=[vpc_id])['Vpcs'][0].get('ClassicLinkEnabled')
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 317, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 615, in _make_api_call
raise error_class(parsed_response, operation_name)
ClientError: An error occurred (InvalidVpcID.NotFound) when calling the DescribeVpcClassicLink operation: The vpc ID 'vpc-1b2bda60' does not exist

# Fixed code
def ensure_final_subnet(conn, module, subnet, start_time):
    for rewait in range(0, 30):
        map_public_correct = False
        assign_ipv6_correct = False

        if module.params["map_public"] == subnet["map_public_ip_on_launch"]:
            map_public_correct = True
        else:
            if module.params["map_public"]:
                handle_waiter(
                    conn,
                    module,
                    "subnet_has_map_public",
                    {"SubnetIds": [subnet["id"]]},
                    start_time,
                )
            else:
                handle_waiter(
                    conn,
                    module,
                    "subnet_no_map_public",
                    {"SubnetIds": [subnet["id"]]},
                    start_time,
                )

        if module.params["assign_instances_ipv6"] == subnet.get(
            "assign_ipv6_address_on_creation"
        ):
            assign_ipv6_correct = True
        else:
            if module.params["assign_instances_ipv6"]:
                handle_waiter(
                    conn,
                    module,
                    "subnet_has_assign_ipv6",
                    {"SubnetIds": [subnet["id"]]},
                    start_time,
                )
            else:
                handle_waiter(
                    conn,
                    module,
                    "subnet_no_assign_ipv6",
                    {"SubnetIds": [subnet["id"]]},
                    start_time,
                )

        if map_public_correct and assign_ipv6_correct:
            break

        time.sleep(5)
        subnet = get_matching_subnet(
            conn, module, module.params["vpc_id"], module.params["cidr"]
        )

    return subnet