【问题标题】:Add a parameterized list of security groups to another security group's ingress将安全组的参数化列表添加到另一个安全组的入口
【发布时间】:2015-11-11 23:16:11
【问题描述】:

我想创建一个 CloudFormation 模板,该模板创建一个安全组资源,允许从其他安全组的变量列表进入。该模板将采用List<AWS::EC2::SecurityGroup::Id> 类型的参数。在本示例中,我将此参数命名为 SourceSecurityGroupIds。然后,它将使用以下内容创建安全组资源:

{
    "LogServerSecurityGroup": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
            "GroupDescription": "XYZ security group",
            "VpcId": "vpc-abcxyz",
            "SecurityGroupIngress": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": 1234,
                    "ToPort": 1234,
                    "SourceSecurityGroupId": { "Ref": "SourceSecurityGroupIds" }
                }
            ]
        }
    }
}

当然,SecurityGroupIngressSourceSecurityGroupId 属性不带列表。有没有办法让这个工作?

更新 - 2019 年 2 月 27 日

回想起来,正确的做法是创建一个LogSourceSecurityGroup,并只允许来自该安全组的进入。然后,将该安全组添加到任何应该能够与日志服务器通信的 EC2 实例等。

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-cloudformation


    【解决方案1】:

    我知道现在已经很晚了,所以你已经弄清楚了,但我刚刚遇到了同样的问题,我能够解决它。您需要创建一个“安全组入口”资源,将规则添加到现有的安全组,所以它会像:

    {
        "LogServerSecurityGroup": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": "XYZ security group",
                "VpcId": "vpc-abcxyz"
            }
        },
        "LogServerSecurityGroupIngress" : {
            "Type" : "AWS::EC2::SecurityGroupIngress",
            "Properties" : { 
                "GroupId" : {"Ref" : "LogServerSecurityGroup"},
                "IpProtocol" : "tcp",
                "FromPort" : "1234",
                "ToPort" : "1234",
                "SourceSecurityGroupId" : {"Ref" : "SourceSecurityGroupIds"}
            }
    
       }
    }
    

    您可以在此处找到更多信息: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group-ingress.html#cfn-ec2-security-group-ingress-groupid

    【讨论】:

      【解决方案2】:

      SecurityGroupIngress 上面的参数是一个数组/列表。所以,在那里定义多个入口规则。

      例如:

        "SecurityGroupIngress": [
          {
            "IpProtocol": "tcp",
            "FromPort": 1234,
            "ToPort": 1234,
            "SourceSecurityGroupId": "SG-12345"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": 1234,
            "ToPort": 1234,
            "SourceSecurityGroupId": "SG-abcde"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": 1234,
            "ToPort": 1234,
            "SourceSecurityGroupId": "SG-54321"
          }
        ]
      

      【讨论】:

      • 您的解决方案不使用由参数指定的安全组的可变列表。
      • @Sean,这样不行。 Read the documentation
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 2021-08-08
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      相关资源
      最近更新 更多