# Buggy code
def validate_distribution_from_caller_reference(self, caller_reference):
    try:
        distributions = self.__cloudfront_facts_mgr.list_distributions(False)
        distribution_name = "Distribution"
        distribution_config_name = "DistributionConfig"
        distribution_ids = [dist.get("Id") for dist in distributions]
        for distribution_id in distribution_ids:
            config = self.__cloudfront_facts_mgr.get_distribution(distribution_id)
            distribution = config.get(distribution_name)
            if distribution is not None:
                distribution_config = distribution.get(distribution_config_name)
                if (
                    distribution_config is not None
                    and distribution_config.get("CallerReference") == caller_reference
                ):
                    distribution["DistributionConfig"] = distribution_config
                    return distribution

    except Exception as e:
        self.module.fail_json_aws(
            e, msg="Error validating distribution from caller reference"
        )


# Traceback (KeyError)
Using module file /usr/local/lib/python3.7/site-packages/ansible/modules/cloud/amazon/cloudfront_distribution.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: avojeez0
<127.0.0.1> EXEC /bin/sh -c '/usr/local/opt/python/bin/python3.7 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/sl/twl1b5l52s9_c4gg0f5jx6m00000gn/T/ansible_cjmalv1o/ansible_module_cloudfront_distribution.py", line 1988, in <module>
main()
File "/var/folders/sl/twl1b5l52s9_c4gg0f5jx6m00000gn/T/ansible_cjmalv1o/ansible_module_cloudfront_distribution.py", line 1918, in main
config = distribution['Distribution']['DistributionConfig']
KeyError: 'Distribution'

fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/sl/twl1b5l52s9_c4gg0f5jx6m00000gn/T/ansible_cjmalv1o/ansible_module_cloudfront_distribution.py\", line 1988, in <module>\n    main()\n  File \"/var/folders/sl/twl1b5l52s9_c4gg0f5jx6m00000gn/T/ansible_cjmalv1o/ansible_module_cloudfront_distribution.py\", line 1918, in main\n    config = distribution['Distribution']['DistributionConfig']\nKeyError: 'Distribution'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def validate_distribution_from_caller_reference(self, caller_reference):
    try:
        distributions = self.__cloudfront_facts_mgr.list_distributions(False)
        distribution_name = "Distribution"
        distribution_config_name = "DistributionConfig"
        distribution_ids = [dist.get("Id") for dist in distributions]
        for distribution_id in distribution_ids:
            distribution = self.__cloudfront_facts_mgr.get_distribution(distribution_id)
            if distribution is not None:
                distribution_config = distribution[distribution_name].get(
                    distribution_config_name
                )
                if (
                    distribution_config is not None
                    and distribution_config.get("CallerReference") == caller_reference
                ):
                    distribution[distribution_name][distribution_config_name] = (
                        distribution_config
                    )
                    return distribution

    except Exception as e:
        self.module.fail_json_aws(
            e, msg="Error validating distribution from caller reference"
        )