Skip to content

Fn::If branches and Condition-gated resources are validated in regions where the condition cannot be true #4746

Open

Description

@hoganbr-amzn

CloudFormation Lint Version

1.57.2

What operating system are you using?

Linux

Describe the bug

A condition of the form !Equals [!Ref "AWS::Region", X] is decidable once --regions is given, but property rules validate both Fn::If branches and every Condition-gated resource regardless. E3006 is the one rule that consults the region (conditions.py build_scenerios_on_region); the others call get_object_without_conditions and build_scenarios without their region argument. Four rules below; E3019 is the lead.

Expected behavior

No findings in the regions shown. A branch or resource the condition rules out for the linted region should not be validated for that region.

Reproduction template

A (E3019)

AWSTemplateFormatVersion: "2010-09-09"
Conditions:
  IsIAD: !Equals [!Ref "AWS::Region", us-east-1]
Resources:
  RoleOne:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !If [IsIAD, role-one-iad, role-shared-elsewhere]
      AssumeRolePolicyDocument: {Version: "2012-10-17", Statement: [{Effect: Allow, Principal: {Service: ec2.amazonaws.com}, Action: "sts:AssumeRole"}]}
  RoleTwo:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !If [IsIAD, role-two-iad, role-shared-elsewhere]
      AssumeRolePolicyDocument: {Version: "2012-10-17", Statement: [{Effect: Allow, Principal: {Service: ec2.amazonaws.com}, Action: "sts:AssumeRole"}]}

--regions us-east-1: E3019 Primary identifiers {'RoleName': 'role-shared-elsewhere'} should have unique values across the resources {'RoleOne', 'RoleTwo'} on both roles. In us-east-1 the names are role-one-iad and role-two-iad. (--regions us-west-2 should and does report it.)

B (E3628, Fn::If)

AWSTemplateFormatVersion: "2010-09-09"
Conditions:
  IsUsEast1: !Equals [us-east-1, !Ref "AWS::Region"]
Resources:
  Host:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-12345678
      InstanceType: !If [IsUsEast1, c4.2xlarge, t3.micro]

--regions ca-west-1: E3628 'c4.2xlarge' is not one of [...]. ca-west-1 takes t3.micro. (us-east-1 is clean.)

C (E3628, Condition)

AWSTemplateFormatVersion: "2010-09-09"
Conditions:
  IsUsEast1: !Equals [us-east-1, !Ref "AWS::Region"]
Resources:
  Host:
    Type: AWS::EC2::Instance
    Condition: IsUsEast1
    Properties:
      ImageId: ami-12345678
      InstanceType: c4.2xlarge

--regions ca-west-1: same E3628 on a resource not created there.

D (E3684)

AWSTemplateFormatVersion: "2010-09-09"
Conditions:
  IsChina: !Equals [!Ref "AWS::Region", cn-north-1]
Resources:
  TG:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      HealthCheckProtocol: !If [IsChina, TCP, HTTPS]
      Matcher:
        HttpCode: !If [IsChina, !Ref "AWS::NoValue", 200-299]
      Port: 443
      Protocol: TCP
      VpcId: vpc-12345678

--regions us-east-1: E3684 Additional properties are not allowed ('Matcher' was unexpected). In us-east-1 the check is HTTPS with HttpCode: 200-299, which is clean when written without the condition.

E (E3502)

AWSTemplateFormatVersion: "2010-09-09"
Conditions:
  IsWest2: !Equals [!Ref "AWS::Region", us-west-2]
Resources:
  StdDlq:
    Type: AWS::SQS::Queue
  FifoDlq:
    Type: AWS::SQS::Queue
    Properties:
      FifoQueue: true
  StdSourceIfRegion:
    Type: AWS::SQS::Queue
    Properties:
      RedrivePolicy:
        deadLetterTargetArn: !If [IsWest2, !GetAtt FifoDlq.Arn, !GetAtt StdDlq.Arn]
        maxReceiveCount: 5
  StdSourceCondFalse:
    Type: AWS::SQS::Queue
    Condition: IsWest2
    Properties:
      RedrivePolicy:
        deadLetterTargetArn: !GetAtt FifoDlq.Arn
        maxReceiveCount: 5
  FifoIfSource:
    Type: AWS::SQS::Queue
    Properties:
      FifoQueue: !If [IsWest2, true, !Ref "AWS::NoValue"]
      RedrivePolicy:
        deadLetterTargetArn: !GetAtt StdDlq.Arn
        maxReceiveCount: 5

--regions us-east-1: three E3502 Source queue type ... does not match destination queue type ... findings; every mismatch is on the IsWest2 branch or resource.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions