# Buggy code
def associate_ip_and_device(
    ec2,
    module,
    address,
    private_ip_address,
    device_id,
    allow_reassociation,
    check_mode,
    is_instance=True,
):
    if address_is_associated_with_device(ec2, module, address, device_id, is_instance):
        return {"changed": False}

    # If we're in check mode, nothing else to do
    if not check_mode:
        if is_instance:
            try:
                params = dict(
                    InstanceId=device_id,
                    PrivateIpAddress=private_ip_address,
                    AllowReassociation=allow_reassociation,
                )
                if address.domain == "vpc":
                    params["AllocationId"] = address["AllocationId"]
                else:
                    params["PublicIp"] = address["PublicIp"]
                res = ec2.associate_address(**params)
            except (
                botocore.exceptions.BotoCoreError,
                botocore.exceptions.ClientError,
            ) as e:
                msg = (
                    "Couldn't associate Elastic IP address with instance '{0}'".format(
                        device_id
                    )
                )
                module.fail_json_aws(e, msg=msg)
        else:
            params = dict(
                NetworkInterfaceId=device_id,
                AllocationId=address["AllocationId"],
                AllowReassociation=allow_reassociation,
            )

            if private_ip_address:
                params["PrivateIpAddress"] = private_ip_address

            try:
                res = ec2.associate_address(aws_retry=True, **params)
            except (
                botocore.exceptions.BotoCoreError,
                botocore.exceptions.ClientError,
            ) as e:
                msg = "Couldn't associate Elastic IP address with network interface '{0}'".format(
                    device_id
                )
                module.fail_json_aws(e, msg=msg)
        if not res:
            module.fail_json_aws(e, msg="Association failed.")

    return {"changed": True}


# 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 associate_ip_and_device(
    ec2,
    module,
    address,
    private_ip_address,
    device_id,
    allow_reassociation,
    check_mode,
    is_instance=True,
):
    if address_is_associated_with_device(ec2, module, address, device_id, is_instance):
        return {"changed": False}

    # If we're in check mode, nothing else to do
    if not check_mode:
        if is_instance:
            try:
                params = dict(
                    InstanceId=device_id,
                    AllowReassociation=allow_reassociation,
                )
                if private_ip_address:
                    params["PrivateIPAddress"] = private_ip_address
                if address["Domain"] == "vpc":
                    params["AllocationId"] = address["AllocationId"]
                else:
                    params["PublicIp"] = address["PublicIp"]
                res = ec2.associate_address(**params)
            except (
                botocore.exceptions.BotoCoreError,
                botocore.exceptions.ClientError,
            ) as e:
                msg = (
                    "Couldn't associate Elastic IP address with instance '{0}'".format(
                        device_id
                    )
                )
                module.fail_json_aws(e, msg=msg)
        else:
            params = dict(
                NetworkInterfaceId=device_id,
                AllocationId=address["AllocationId"],
                AllowReassociation=allow_reassociation,
            )

            if private_ip_address:
                params["PrivateIpAddress"] = private_ip_address

            try:
                res = ec2.associate_address(aws_retry=True, **params)
            except (
                botocore.exceptions.BotoCoreError,
                botocore.exceptions.ClientError,
            ) as e:
                msg = "Couldn't associate Elastic IP address with network interface '{0}'".format(
                    device_id
                )
                module.fail_json_aws(e, msg=msg)
        if not res:
            module.fail_json_aws(e, msg="Association failed.")

    return {"changed": True}