【问题标题】:CloudFormation SecurityGroup Circular ReferenceCloudFormation SecurityGroup 循环参考
【发布时间】:2018-04-08 07:19:09
【问题描述】:

我正在使用两个需要相互通信的简单网络应用程序。在 AWS CloudFormation 中,我有一个创建 EC2 实例并将两个应用程序安装在同一服务器上的模板(最终我会将它们拆分,但现在它们位于同一个 EC2 实例上)。

作为 EC2 实例的一部分,我必须定义要使用的 SecurityGroup。现在我一直在使用默认的,但我想动态构建一个。在组中,我允许从我的机器进行 SSH 访问,并允许从盒子到自身的一些端口。

当使用默认组时,我可以将服务器的公共 ip 添加到它自己的安全组中,以允许它与自己通信。问题是在 CloudFormation 模板期间,我得到了 SecurityGroup 和 EC2 实例之间的循环引用。实例需要一个 SecurityGroup 才能启动,并且该组需要包含 EC2 框的公共 IP 规则。

有没有更好的方法来做到这一点,或者以某种方式锁定“本地主机”的内容以暂时允许这些流量进入​​?

【问题讨论】:

  • 使用默认或动态创建的安全组创建实例后,您可以创建一个 AWS::EC2::SecurityGroupIngress 对象,该对象可以引用您的 EC2 实例(我认为)。 docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
  • 成功了。它允许 EC2 与非空组一起启动。然后在初始化后连接IP。再次感谢!
  • 文档描述了这种情况:如果您想在这些安全组的入口和出口规则中交叉引用两个安全组,请使用 AWS::EC2::SecurityGroupEgress 和 AWS::EC2: :SecurityGroupIngress 资源来定义您的规则。不要使用 AWS::EC2::SecurityGroup 中的嵌入式入口和出口规则。这样做会创建一个循环依赖,这是 AWS CloudFormation 不允许的。

标签: amazon-web-services amazon-ec2 amazon-cloudformation aws-security-group


【解决方案1】:

你有几个选择:

  1. 您可以为此设置一个自引用安全组,这意味着 EC2 实例将被允许与其自身通信,因为它位于该安全组上。有一个警告,那就是不要在 AWS::EC2::SecurityGroup 中使用嵌入式入口和出口规则,而是单独使用 AWS::EC2::SecurityGroupEgressAWS::EC2::SecurityGroupIngress,如 here 所述。

看起来像:

"Resources" : {
    "SelfRefSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Has access to itself",
        "VpcId" : "vpc-xxxxxx"
      }
    },
    "MySecurityGroupIngress" : {
         "Type" : "AWS::EC2::SecurityGroupIngress",
         "Properties" : {
             "GroupId" : { "Ref" : "SelfRefSecurityGroup" },
             "IpProtocol" : "tcp",
             "ToPort" : "65535",
             "FromPort" : "0",
             "SourceSecurityGroupId" : { "Ref" : "SelfRefSecurityGroup" }
         },
         "DependsOn" : "SelfRefSecurityGroup"
     }
  1. 最佳选择: 在框中创建 2 个主机条目(或者最好使用 Route53 private hosted zone 设置 dns 条目):

    webapp1.com 127.0.0.1

    webapp2.com 127.0.0.1

我不建议让盒子通过其公共 IP 与自己对话。可能你甚至会产生成本! (https://aws.amazon.com/ec2/pricing/on-demand/) 加上 SG 的额外维护。

【讨论】:

    【解决方案2】:

    我们可以在创建过程中自行引用安全组:

    ---
    Description: Create a VPC with a SG which references itself
    AWSTemplateFormatVersion: '2010-09-09'
    Resources:
      vpctester:
        Type: AWS::EC2::VPC
        Properties:
          CidrBlock: 172.16.0.0/23
          EnableDnsSupport: false
          EnableDnsHostnames: false
          InstanceTenancy: default
          Tags:
          - Key: Name
            Value: vpctester
      sgtester:
        Type: AWS::EC2::SecurityGroup
        DependsOn: vpctester
        Properties:
          GroupDescription: vpc tester sg
          VpcId:
            Ref: vpctester
      sgtesteringress:
        Type: AWS::EC2::SecurityGroupIngress
        DependsOn: sgtester
        Properties:
          GroupId:
            Ref: sgtester
          IpProtocol: tcp
          FromPort: '0'
          ToPort: '65535'
          SourceSecurityGroupId:
            Ref: sgtester
    
    

    请阅读我的帖子了解更多信息: https://dev.to/anupamncsu/self-referencing-security-groups-on-aws-59gb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 2018-10-01
      • 1970-01-01
      相关资源
      最近更新 更多