aws-cloudformation / cfn-lint

CloudFormation Linter
MIT No Attribution
2.4k stars 576 forks source link

E1017, E1019 missing key reported when value is falsy #3408

Closed nosnilmot closed 6 days ago

nosnilmot commented 6 days ago

CloudFormation Lint Version

1.3.5

What operating system are you using?

Mac

Describe the bug

Thanks for fixing #3385

Now cfn-lint incorrectly complains about missing keys if the resolved value is falsy (0 or "")

E1019 'Idx' is not one of ['Idx'] for mapping 'Map' and key 'Key' when 'Ref' is resolved
map.yaml:10:7

E1019 'Idx' is not one of ['Idx'] for mapping 'Map' and key 'Key'
map.yaml:12:45

Expected behavior

no errors or warnings

Reproduction template

AWSTemplateFormatVersion: '2010-09-09'
Mappings:
  Map:
    Key:
      Idx: 0
Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub
        - "bucket-${number}"
        - number: !FindInMap ["Map", "Key", "Idx"]
nosnilmot commented 6 days ago

Another variant of the same problem reports as E1017 (this is closer to what I distilled to the original reproducer but I didn't notice the error number changed):

E1017 'Idx' is not one of ['Idx'] for mapping 'Map' and key 'Key'
mapv.yaml:25:13
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  CidrBlock:
    Type: String
    Default: "192.168.0.0/24"
  Ipv6CidrList:
    Type: CommaDelimitedList
    Default: ""
Mappings:
  Map:
    Key:
      Idx: 0
Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref CidrBlock
  Subnet:
    Type: AWS::EC2::Subnet
    Properties:
      Ipv6CidrBlock: !Select
        - !FindInMap
          - Map
          - Key
          - Idx
        - !Ref Ipv6CidrList
      VpcId: !Ref 'VPC'
nosnilmot commented 6 days ago

Another FindInMap problem (do you need a separate issue for this?)

cfn-lint 1.3.0-1.3.5 do not report any problem with this template that is completely missing the map RegionMap

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !FindInMap
        - RegionMap
        - !Ref 'AWS::Region'
        - Key