aws-cloudformation / cfn-lint

CloudFormation Linter
MIT No Attribution
2.43k stars 591 forks source link

resources and parameters must have unique names #1689

Closed PatMyron closed 4 years ago

PatMyron commented 4 years ago
Parameters:
  Name:
    Type: String
Resources:
  Name:
    Type: AWS::SNS::Topic
Template error: all resources and parameters must have unique names. Common name(s):[Name]
kddejong commented 4 years ago

Is that now an error in the service itself? I've been thinking about this for a while. It used to be that didn't error out (when I tested it). I wanted to create it as a Warning since a Ref to the name could yield weird results. I'm actually glad this is an error and this should be an easy rule to write.

PatMyron commented 4 years ago

Errors out in the service itself, think I'm almost done with the rule, just want to test before opening PR:

"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from cfnlint.rules import CloudFormationLintRule, RuleMatch

class UniqueNames(CloudFormationLintRule):
    id = ''
    shortdesc = 'Unique resources and parameters'
    description = 'All resources and parameters must have unique names'
    source_url = ''
    tags = ['parameters', 'resources']

    def match(self, cfn):
        matches = []
        for resource in cfn.get_resources():
            for parameter in cfn.template.get('Parameters', {}):
                if resource == parameter:
                    matches.append(RuleMatch(['Resources', resource], 'Resources and Parameters must not share name: ' + resource))
        return matches

@kddejong also which directory / rule namespace should this one go into? 🤔