【问题标题】:Allow every instance in the same Security Group to share any data between each other at Cloud Formation JSON?允许同一安全组中的每个实例在 Cloud Formation JSON 上相互共享任何数据?
【发布时间】:2015-01-03 01:33:28
【问题描述】:

我正在构建一个 Cloud Formation JSON 来定义 EC2 实例和安全组。

我需要创建一个安全组,允许属于其中的每个实例在彼此之间共享任何数据。

我的 JSON 是这样的:

"InternalSecurityGroup" : {
  "Type" : "AWS::EC2::SecurityGroup",
  "Properties" : {
    "VpcId" : {"Ref" : "myVPC"},
    "GroupDescription" : "Allow the machines in this group to share all kinds of traffic between each other",
    "SecurityGroupIngress" : [
      {
        "IpProtocol" : "-1",
        "FromPort": "-1",
        "ToPort": "-1",
        "SourceSecurityGroupId" : { "Ref" : "InternalSecurityGroup" }
      }
    ],
    "SecurityGroupEgress" : [
      {
        "IpProtocol" : "-1",
        "FromPort": "-1",
        "ToPort": "-1",
        "DestinationSecurityGroupId" : { "Ref" : "InternalSecurityGroup" }
      }
    ]

  }
},

但这显示了以下错误:

调用 CreateStack 时发生客户端错误(ValidationError) 操作:资源之间的循环依赖

为了解决这个问题,我将代码更改为 CidrIp 而不是 SourceSecurityGroupId,定义了实例所在的子网。

是否可以引用同一个安全组?实现我想要的最好(或正确)方法是什么?

【问题讨论】:

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


    【解决方案1】:

    documentation 中所述,您可以使用AWS::EC2::SecurityGroupEgressAWS::EC2::SecurityGroupIngress 资源来定义自引用安全组规则:

    重要

    如果您想在这些安全组的入口和出口规则中交叉引用两个安全组,请使用 AWS::EC2::SecurityGroupEgressAWS::EC2::SecurityGroupIngress 资源来定义您的规则。不要使用AWS::EC2::SecurityGroup 中嵌入的入口和出口规则。如果这样做,则会导致循环依赖,这是 AWS CloudFormation 所不允许的。

    结果如下:

    {
       "Resources":{
          "myVPC":{
             "Type":"AWS::EC2::VPC",
             "Properties":{
                "CidrBlock":"10.0.0.0/16"
             }
          },
          "InternalSecurityGroup":{
             "Type":"AWS::EC2::SecurityGroup",
             "Properties":{
                "VpcId":{
                   "Ref":"myVPC"
                },
                "GroupDescription":"Allow the machines in this group to share all kinds of traffic between each other"
             }
          },
          "InternalSecurityGroupIngress":{
             "Type":"AWS::EC2::SecurityGroupIngress",
             "Properties":{
                "IpProtocol":"-1",
                "FromPort":"-1",
                "ToPort":"-1",
                "SourceSecurityGroupId":{
                   "Ref":"InternalSecurityGroup"
                },
                "GroupId":{
                   "Ref":"InternalSecurityGroup"
                }
             }
          },
          "InternalSecurityGroupEgress":{
             "Type":"AWS::EC2::SecurityGroupEgress",
             "Properties":{
                "IpProtocol":"-1",
                "FromPort":"-1",
                "ToPort":"-1",
                "DestinationSecurityGroupId":{
                   "Ref":"InternalSecurityGroup"
                },
                "GroupId":{
                   "Ref":"InternalSecurityGroup"
                }
             }
          }
       }
    }
    

    【讨论】:

      【解决方案2】:

      定义两个安全组,这样应该会更好一些:

       "InternalSecurityGroup1" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : {
          "VpcId" : {"Ref" : "myVPC"},
          "GroupDescription" : "Allow the machines in this group to share all kinds of traffic between each other",
          "SecurityGroupIngress" : [ {
              "IpProtocol" : "-1",
              "FromPort": "-1",
              "ToPort": "-1",
              "SourceSecurityGroupId" : { "Ref" : "InternalSecurityGroup2" }
            }
          ]
        }
      }
      
      
      "InternalSecurityGroup2" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : {
          "VpcId" : {"Ref" : "myVPC"},
          "GroupDescription" : "Allow the machines in this group to share all kinds of traffic between each other",
          "SecurityGroupIngress" : [ {
              "IpProtocol" : "-1",
              "FromPort": "-1",
              "ToPort": "-1",
              "SourceSecurityGroupId" : { "Ref" : "InternalSecurityGroup1" }
            }
          ]
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-24
        • 2011-10-23
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        相关资源
        最近更新 更多