# Buggy code
def transform_template(self):
    """
    Transform the Template using the Serverless Application Model.
    """
    matches = []

    try:
        sam_translator = Translator(
            managed_policy_map=self._managed_policy_map, sam_parser=self._sam_parser
        )

        self._replace_local_codeuri()

        # Tell SAM to use the region we're linting in, this has to be controlled using the default AWS mechanisms, see also:
        # https://github.com/awslabs/serverless-application-model/blob/master/samtranslator/translator/arn_generator.py
        os.environ["AWS_DEFAULT_REGION"] = self._region

        # In the Paser class, within the SAM Translator, they log a warning for when the template
        # does not match the schema. The logger they use is the root logger instead of one scoped to
        # their module. Currently this does not cause templates to fail, so we will suppress this
        # by patching the logging.warning method that is used in that class.
        class WarningSuppressLogger(object):
            """Patch the Logger in SAM"""

            def __init__(self, obj_to_patch):
                self.obj_to_patch = obj_to_patch

            def __enter__(self):
                self.obj_to_patch.warning = self.warning

            def __exit__(self, exc_type, exc_val, exc_tb):
                self.obj_to_patch.warning = self.obj_to_patch.warning

            def warning(self, message):
                """Ignore warnings from SAM"""
                pass

        with WarningSuppressLogger(parser.logging):
            self._template = cfnlint.helpers.convert_dict(
                sam_translator.translate(
                    sam_template=self._template, parameter_values={}
                )
            )
    except InvalidDocumentException as e:
        for cause in e.causes:
            matches.append(
                cfnlint.Match(
                    1, 1, 1, 1, self._filename, cfnlint.TransformError(), cause.message
                )
            )

    return matches


# Traceback (KeyError)
$ cfn-lint -t template.yaml
Traceback (most recent call last):
File "/Users/emilbryggare/Library/Python/2.7/bin/cfn-lint", line 11, in <module>
sys.exit(main())
File "/Users/emilbryggare/Library/Python/2.7/lib/python/site-packages/cfnlint/__main__.py", line 36, in main
args.regions, args.override_spec))
File "/Users/emilbryggare/Library/Python/2.7/lib/python/site-packages/cfnlint/core.py", line 46, in run_cli
return run_checks(filename, template, rules, regions)
File "/Users/emilbryggare/Library/Python/2.7/lib/python/site-packages/cfnlint/core.py", line 316, in run_checks
matches.extend(runner.transform())
File "/Users/emilbryggare/Library/Python/2.7/lib/python/site-packages/cfnlint/__init__.py", line 894, in transform
matches = transform.transform_template()
File "/Users/emilbryggare/Library/Python/2.7/lib/python/site-packages/cfnlint/transform.py", line 115, in transform_template
sam_translator.translate(sam_template=self._template, parameter_values={}))
File "/Users/emilbryggare/Library/Python/2.7/lib/python/site-packages/samtranslator/translator/translator.py", line 72, in translate
translated = macro.to_cloudformation(**kwargs)
File "/Users/emilbryggare/Library/Python/2.7/lib/python/site-packages/samtranslator/model/sam_resources.py", line 537, in to_cloudformation
dynamodb_resources = self._construct_dynamodb_table()
File "/Users/emilbryggare/Library/Python/2.7/lib/python/site-packages/samtranslator/model/sam_resources.py", line 546, in _construct_dynamodb_table
'AttributeName': self.PrimaryKey['Name'],
KeyError: 'Name'

# Fixed code
def transform_template(self):
    """
    Transform the Template using the Serverless Application Model.
    """
    matches = []

    try:
        sam_translator = Translator(
            managed_policy_map=self._managed_policy_map, sam_parser=self._sam_parser
        )

        self._replace_local_codeuri()

        # Tell SAM to use the region we're linting in, this has to be controlled using the default AWS mechanisms, see also:
        # https://github.com/awslabs/serverless-application-model/blob/master/samtranslator/translator/arn_generator.py
        os.environ["AWS_DEFAULT_REGION"] = self._region

        # In the Paser class, within the SAM Translator, they log a warning for when the template
        # does not match the schema. The logger they use is the root logger instead of one scoped to
        # their module. Currently this does not cause templates to fail, so we will suppress this
        # by patching the logging.warning method that is used in that class.
        class WarningSuppressLogger(object):
            """Patch the Logger in SAM"""

            def __init__(self, obj_to_patch):
                self.obj_to_patch = obj_to_patch

            def __enter__(self):
                self.obj_to_patch.warning = self.warning

            def __exit__(self, exc_type, exc_val, exc_tb):
                self.obj_to_patch.warning = self.obj_to_patch.warning

            def warning(self, message):
                """Ignore warnings from SAM"""
                pass

        with WarningSuppressLogger(parser.logging):
            self._template = cfnlint.helpers.convert_dict(
                sam_translator.translate(
                    sam_template=self._template, parameter_values={}
                )
            )
    except InvalidDocumentException as e:
        for cause in e.causes:
            matches.append(
                cfnlint.Match(
                    1, 1, 1, 1, self._filename, cfnlint.TransformError(), cause.message
                )
            )
    except Exception as e:  # pylint: disable=W0703
        LOGGER.debug("Error transforming template: %s", str(e))
        LOGGER.debug("Stack trace: %s", e, exc_info=True)
        message = "Error transforming template: {0}"
        matches.append(
            cfnlint.Match(
                1,
                1,
                1,
                1,
                self._filename,
                cfnlint.TransformError(),
                message.format(str(e)),
            )
        )

    return matches