# Buggy code
def get_invalidation(self, distribution_id, caller_reference):
    current_invalidation = {}
    # find all invalidations for the distribution
    try:
        paginator = self.client.get_paginator("list_invalidations")
        invalidations = (
            paginator.paginate(DistributionId=distribution_id)
            .build_full_result()["InvalidationList"]
            .get("Items", [])
        )
        invalidation_ids = [inv["Id"] for inv in invalidations]
    except (BotoCoreError, ClientError) as e:
        self.module.fail_json_aws(e, msg="Error listing CloudFront invalidations.")

    # check if there is an invalidation with the same caller reference
    for inv_id in invalidation_ids:
        try:
            invalidation = self.client.get_invalidation(
                DistributionId=distribution_id, Id=inv_id
            )["Invalidation"]
            caller_ref = invalidation.get("InvalidationBatch", {}).get(
                "CallerReference"
            )
        except (BotoCoreError, ClientError) as e:
            self.module.fail_json_aws(
                e, msg="Error getting Cloudfront invalidation {0}".format(inv_id)
            )
        if caller_ref == caller_reference:
            current_invalidation = invalidation
            break

    current_invalidation.pop("ResponseMetadata", None)
    return current_invalidation


# Traceback (nKeyError)
fatal: [XXX]: FAILED! => {
"changed": false,
"module_stderr": "Shared connection to 1.2.3.4 closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_7NNXVV/ansible_module__cloudfront_invalidation.py\", line 275, in <modul
e>\r\n    main()\r\n  File \"/tmp/ansible_7NNXVV/ansible_module__cloudfront_invalidation.py\", line 269, in main\r\n    result, changed = service_mgr.crea
te_invalidation(distribution_id, valid_pascal_target_paths)\r\n  File \"/tmp/ansible_7NNXVV/ansible_module__cloudfront_invalidation.py\", line 160, in cre
ate_invalidation\r\n    current_invalidation_response = self.get_invalidation(distribution_id, invalidation_batch['CallerReference'])\r\n  File \"/tmp/ans
ible_7NNXVV/ansible_module__cloudfront_invalidation.py\", line 184, in get_invalidation\r\n    invalidations = paginator.paginate(DistributionId=distribut
ion_id).build_full_result()['InvalidationList'].get('Items', [])\r\nKeyError: 'InvalidationList'\r\n",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def get_invalidation(self, distribution_id, caller_reference):
    current_invalidation = {}
    # find all invalidations for the distribution
    try:
        paginator = self.client.get_paginator("list_invalidations")
        invalidations = (
            paginator.paginate(DistributionId=distribution_id)
            .build_full_result()
            .get("InvalidationList", {})
            .get("Items", [])
        )
        invalidation_ids = [inv["Id"] for inv in invalidations]
    except (BotoCoreError, ClientError) as e:
        self.module.fail_json_aws(e, msg="Error listing CloudFront invalidations.")

    # check if there is an invalidation with the same caller reference
    for inv_id in invalidation_ids:
        try:
            invalidation = self.client.get_invalidation(
                DistributionId=distribution_id, Id=inv_id
            )["Invalidation"]
            caller_ref = invalidation.get("InvalidationBatch", {}).get(
                "CallerReference"
            )
        except (BotoCoreError, ClientError) as e:
            self.module.fail_json_aws(
                e, msg="Error getting Cloudfront invalidation {0}".format(inv_id)
            )
        if caller_ref == caller_reference:
            current_invalidation = invalidation
            break

    current_invalidation.pop("ResponseMetadata", None)
    return current_invalidation