cloudformation-coverage-roadmap icon indicating copy to clipboard operation
cloudformation-coverage-roadmap copied to clipboard

AWS::EC2::SecurityGroupIngress - resource type ignores "Condition" resource property

Open emibelli opened this issue 4 years ago • 3 comments

Name of the resource

AWS::EC2::SecurityGroupIngress

Resource Name

AWS::EC2::SecurityGroupIngress

Issue Description

Whenever using the Condition resource property in a AWS::EC2::SecurityGroupIngress type resource, the condition is ignored and CloudFormation tries to create/update the resource even though the Condition resource property evaluates to false.

Expected Behavior

The Condition resource property SHOULD NOT be ignored and CloudFormation should create/update the AWS::EC2::SecurityGroupIngress resource based on the Condition resource property accordingly.

Observed Behavior

Stack creation/update fails with the following error:

DatabaseSecurityGroupIngress CREATE_FAILED The security-group ID '' is malformed (Service: AmazonEC2; Status Code: 400; Error Code: InvalidGroupId.Malformed; Request ID: [redacted]; Proxy: null)

Test Cases

Example template:

AWSTemplateFormatVersion: "2010-09-09"

Description: "AWS::EC2::SecurityGroupIngress issue sample template"

Parameters:
  DatabaseSecurityGroupId:
    Type: "String"
    Description: "Database security group ID"
    Default: ""
  DatabasePort:
    Type: "Number"
    Description: "Database port"
    Default: 0
  VpcId:
    Type: "AWS::EC2::VPC::Id"
    Description: "VPC ID"

Conditions:
  HasDatabaseSecurityGroupId: !Not [ !Equals [ "DatabaseSecurityGroupId", "" ] ]
  HasDatabasePort: !Not [ !Equals [ "DatabasePort", 0 ] ]
  HasDatabaseConnection: !And
    - Condition: "HasDatabaseSecurityGroupId"
    - Condition: "HasDatabasePort"

Resources:
  SecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupName: !Sub "${AWS::StackName}-security-group"
      GroupDescription: !Sub "${AWS::StackName} security group"
      VpcId: !Ref "VpcId"
  DatabaseSecurityGroupIngress:
    Condition: "HasDatabaseConnection"
    Type: "AWS::EC2::SecurityGroupIngress"
    Properties:
      GroupId: !Ref "DatabaseSecurityGroupId"
      IpProtocol: "tcp"
      FromPort: !Ref "DatabasePort"
      ToPort: !Ref "DatabasePort"
      SourceSecurityGroupId: !Ref "SecurityGroup"

Other Details

No response

emibelli avatar Jan 25 '22 02:01 emibelli

Bump

yuripourre avatar Jun 14 '22 19:06 yuripourre

As far as I can tell, Conditions are working for AWS::EC2::SecurityGroupIngress. Looking at your template, I noticed these lines:

Conditions:
  HasDatabaseSecurityGroupId: !Not [ !Equals [ "DatabaseSecurityGroupId", "" ] ]
  HasDatabasePort: !Not [ !Equals [ "DatabasePort", 0 ] ]

The variables there need to be prefixed with !Ref:

  HasDatabaseSecurityGroupId: !Not [ !Equals [ !Ref "DatabaseSecurityGroupId", "" ] ]
  HasDatabasePort: !Not [ !Equals [ !Ref "DatabasePort", 0 ] ]

Otherwise, CloudFormation will interpret them as string literals.

When I tested with a much-simplified version of your template:

Parameters:
  DatabasePort:
    Type: "Number"
    Description: "Database port"
    Default: 0
  VpcId:
    Type: "AWS::EC2::VPC::Id"
    Description: "VPC ID"
    Default: ""

Conditions:
  AlwaysFalseCondition: !Not [ !Equals [ !Ref DatabasePort, 0 ] ]

Resources:
  SecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupName: !Sub "${AWS::StackName}-security-group"
      GroupDescription: !Sub "${AWS::StackName} security group"
      VpcId: !Ref "VpcId"
  DatabaseSecurityGroupIngress:
    Condition: "AlwaysFalseCondition"
    Type: "AWS::EC2::SecurityGroupIngress"
    Properties:
      GroupId: !Ref "SecurityGroup"
      IpProtocol: "tcp"
      FromPort: !Ref "DatabasePort"
      ToPort: !Ref "DatabasePort"
      SourceSecurityGroupId: !Ref "SecurityGroup"
      Description: !Sub "${AWS::StackName}-security-group-rule"

CloudFormation correctly omits the rule:

% aws ec2 describe-security-groups --group-ids <security group ID> --region $REGION
{
    "SecurityGroups": [
        {
            "Description": "stack-repro security group",
            "GroupName": "stack-repro-security-group",
            "IpPermissions": [],
            "OwnerId": "<account ID>",
            "GroupId": "<security group ID>",
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": []
                }
            ],
            "Tags": [
                {
                    "Key": "aws:cloudformation:logical-id",
                    "Value": "SecurityGroup"
                },
                {
                    "Key": "aws:cloudformation:stack-id",
                    "Value": "arn:aws:cloudformation:us-east-1:<account ID>:stack/stack-repro/84f4a0c0-2f7c-11f0-bb46-12fd9e949d9b"
                },
                {
                    "Key": "aws:cloudformation:stack-name",
                    "Value": "stack-repro"
                }
            ],
            "VpcId": "<VPC ID>",
            "SecurityGroupArn": "arn:aws:ec2:us-east-1:<account ID>:security-group/<security group ID>"
        }
    ]
}

I'm going to resolve this issue, but please either reopen it or create a new issue if you need anything else (or if I've missed something).

davidataws avatar May 14 '25 19:05 davidataws

Closing this out based on @davidataws 's comment above. Please let us know if the issue resurfaces or if you have questions.

shantgup avatar May 14 '25 19:05 shantgup