# Buggy code
def compare_subnets(self):
    """
    Compare user subnets with current ELB subnets

    :return: bool True if they match otherwise False
    """

    subnet_mapping_id_list = []
    subnet_mappings = []

    # Check if we're dealing with subnets or subnet_mappings
    if self.subnets is not None:
        # Convert subnets to subnet_mappings format for comparison
        for subnet in self.subnets:
            subnet_mappings.append({"SubnetId": subnet})

    if self.subnet_mappings is not None:
        # Use this directly since we're comparing as a mapping
        subnet_mappings = self.subnet_mappings

    # Build a subnet_mapping style struture of what's currently
    # on the load balancer
    for subnet in self.elb["AvailabilityZones"]:
        this_mapping = {"SubnetId": subnet["SubnetId"]}
        for address in subnet["LoadBalancerAddresses"]:
            if "AllocationId" in address:
                this_mapping["AllocationId"] = address["AllocationId"]
                break

        subnet_mapping_id_list.append(this_mapping)

    return set(frozenset(mapping.items()) for mapping in subnet_mapping_id_list) == set(
        frozenset(mapping.items()) for mapping in subnet_mappings
    )


# Traceback (KeyError)
TASK [ELB (Application)] ***********************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: 'LoadBalancerAddresses'
fatal: [localhost -> localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/home/cho/.ansible/tmp/ansible-tmp-1544029326.15-8855540780494/AnsiballZ_elb_application_lb.py\", line 113, in <module>\n    _ansiballz_main()\n  File \"/home/cho/.ansible/tmp/ansible-tmp-1544029326.15-8855540780494/AnsiballZ_elb_application_lb.py\", line 105, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/cho/.ansible/tmp/ansible-tmp-1544029326.15-8855540780494/AnsiballZ_elb_application_lb.py\", line 48, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_elb_application_lb_payload_9PFpmx/__main__.py\", line 574, in <module>\n  File \"/tmp/ansible_elb_application_lb_payload_9PFpmx/__main__.py\", line 568, in main\n  File \"/tmp/ansible_elb_application_lb_payload_9PFpmx/__main__.py\", line 390, in create_or_update_elb\n  File \"/tmp/ansible_elb_application_lb_payload_9PFpmx/ansible_elb_application_lb_payload.zip/ansible/module_utils/aws/elbv2.py\", line 172, in compare_subnets\nKeyError: 'LoadBalancerAddresses'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

# Fixed code
def compare_subnets(self):
    """
    Compare user subnets with current ELB subnets

    :return: bool True if they match otherwise False
    """

    subnet_mapping_id_list = []
    subnet_mappings = []

    # Check if we're dealing with subnets or subnet_mappings
    if self.subnets is not None:
        # Convert subnets to subnet_mappings format for comparison
        for subnet in self.subnets:
            subnet_mappings.append({"SubnetId": subnet})

    if self.subnet_mappings is not None:
        # Use this directly since we're comparing as a mapping
        subnet_mappings = self.subnet_mappings

    # Build a subnet_mapping style struture of what's currently
    # on the load balancer
    for subnet in self.elb["AvailabilityZones"]:
        this_mapping = {"SubnetId": subnet["SubnetId"]}
        for address in subnet.get("LoadBalancerAddresses", []):
            if "AllocationId" in address:
                this_mapping["AllocationId"] = address["AllocationId"]
                break

        subnet_mapping_id_list.append(this_mapping)

    return set(frozenset(mapping.items()) for mapping in subnet_mapping_id_list) == set(
        frozenset(mapping.items()) for mapping in subnet_mappings
    )