# Buggy code
def find_address(ec2, module, public_ip, device_id, is_instance=True):
    """Find an existing Elastic IP address"""
    filters = []
    kwargs = {}

    if public_ip:
        kwargs["PublicIps"] = [public_ip]
    elif device_id:
        if is_instance:
            filters.append({"Name": "instance-id", "Values": [device_id]})
        else:
            filters.append({"Name": "network-interface-id", "Values": [device_id]})

    if len(filters) > 0:
        kwargs["Filters"] = filters
    elif len(filters) == 0 and public_ip is None:
        return None

    try:
        addresses = ec2.describe_addresses(**kwargs)
    except is_boto3_error_code("InvalidAddress.NotFound") as e:
        module.fail_json_aws(
            e, msg="Couldn't obtain list of existing Elastic IP addresses"
        )

    addresses = addresses["Addresses"]
    if len(addresses) == 1:
        return addresses[0]
    elif len(addresses) > 1:
        msg = "Found more than one address using args {0}".format(kwargs)
        msg += "Addresses found: {0}".format(addresses)
        module.fail_json_aws(botocore.exceptions.ClientError, msg=msg)


# Traceback (botocore.exceptions.ClientError)
TASK [ec2_eip : Rerelease eip (no change)] *************************************
task path: /root/.ansible/test/tmp/ec2_eip-4v9k8sww-ÅÑŚÌβŁÈ/test/integration/targets/ec2_eip/tasks/main.yml:326
<testhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<testhost> EXEC /bin/sh -c 'echo ~root &amp;&amp; sleep 0'
<testhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1568552830.3837097-100844260545029 `" &amp;&amp; echo ansible-tmp-1568552830.3837097-100844260545029="` echo /root/.ansible/tmp/ansible-tmp-1568552830.3837097-100844260545029 `" ) &amp;&amp; sleep 0'
Using module file /root/ansible/lib/ansible/modules/cloud/amazon/ec2_eip.py
<testhost> PUT /root/.ansible/tmp/ansible-local-126l_udgy92/tmppojiib6x TO /root/.ansible/tmp/ansible-tmp-1568552830.3837097-100844260545029/AnsiballZ_ec2_eip.py
<testhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1568552830.3837097-100844260545029/ /root/.ansible/tmp/ansible-tmp-1568552830.3837097-100844260545029/AnsiballZ_ec2_eip.py &amp;&amp; sleep 0'
<testhost> EXEC /bin/sh -c 'ANSIBLE_DEBUG_BOTOCORE_LOGS=True /tmp/python-tkg1ink4-ansible/python /root/.ansible/tmp/ansible-tmp-1568552830.3837097-100844260545029/AnsiballZ_ec2_eip.py &amp;&amp; sleep 0'
<testhost> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1568552830.3837097-100844260545029/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_ec2_eip_payload_jseaw6r8/ansible_ec2_eip_payload.zip/ansible/modules/cloud/amazon/ec2_eip.py", line 317, in find_address
File "/tmp/ansible_ec2_eip_payload_jseaw6r8/ansible_ec2_eip_payload.zip/ansible/module_utils/aws/core.py", line 283, in deciding_wrapper
return unwrapped(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 661, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidAddress.NotFound) when calling the DescribeAddresses operation: Address '52.43.70.125' not found.

fatal: [testhost]: FAILED! => {
"boto3_version": "1.9.204",
"botocore_version": "1.12.204",
"changed": false,
"error": {
"code": "InvalidAddress.NotFound",
"message": "Address '52.43.70.125' not found."
},
"invocation": {
"module_args": {
"allow_reassociation": false,
"aws_access_key": "REDACTED",
"aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"debug_botocore_endpoint_logs": true,
"device_id": null,
"ec2_url": null,
"in_vpc": true,
"private_ip_address": null,
"profile": null,
"public_ip": "52.43.70.125",
"public_ipv4_pool": null,
"region": "us-west-2",
"release_on_disassociation": false,
"reuse_existing_ip_allowed": false,
"security_token": null,
"state": "absent",
"tag_name": null,
"tag_value": null,
"validate_certs": true,
"wait_timeout": 300
}
},
"msg": "Couldn't obtain list of existing Elastic IP addresses: An error occurred (InvalidAddress.NotFound) when calling the DescribeAddresses operation: Address '52.43.70.125' not found.",
"resource_actions": [
"ec2:DescribeAddresses"
],
"response_metadata": {
"http_headers": {
"connection": "close",
"date": "Sun, 15 Sep 2019 13:07:11 GMT",
"server": "AmazonEC2",
"transfer-encoding": "chunked"
},
"http_status_code": 400,
"request_id": "c4b65cd8-02de-4503-af3a-0e498db40d53",
"retry_attempts": 0
}
}

# Fixed code
def find_address(ec2, module, public_ip, device_id, is_instance=True):
    """Find an existing Elastic IP address"""
    filters = []
    kwargs = {}

    if public_ip:
        kwargs["PublicIps"] = [public_ip]
    elif device_id:
        if is_instance:
            filters.append({"Name": "instance-id", "Values": [device_id]})
        else:
            filters.append({"Name": "network-interface-id", "Values": [device_id]})

    if len(filters) > 0:
        kwargs["Filters"] = filters
    elif len(filters) == 0 and public_ip is None:
        return None

    try:
        addresses = ec2.describe_addresses(**kwargs)
    except is_boto3_error_code("InvalidAddress.NotFound") as e:
        # If we're releasing and we can't find it, it's already gone...
        if module.params.get("state") == "absent":
            module.exit_json(changed=False)
        module.fail_json_aws(
            e, msg="Couldn't obtain list of existing Elastic IP addresses"
        )

    addresses = addresses["Addresses"]
    if len(addresses) == 1:
        return addresses[0]
    elif len(addresses) > 1:
        msg = "Found more than one address using args {0}".format(kwargs)
        msg += "Addresses found: {0}".format(addresses)
        module.fail_json_aws(botocore.exceptions.ClientError, msg=msg)