CloudFormation SecurityGroup循环参考

Wal*_*lls 2 amazon-ec2 amazon-web-services aws-cloudformation

我正在使用两个需要互相交谈的简单Web应用程序。在AWS CloudFormation中,我有一个模板,该模板创建一个EC2实例并将这两个应用程序安装在同一服务器上(最终,我将它们拆分开来,但目前它们都位于同一EC2实例上)。

作为EC2实例的一部分,我必须定义要使用的SecurityGroup。目前,我一直在使用默认的,但是我想动态地构建一个。在该组中,我允许从我的计算机输入SSH,并允许从包装盒到自身的一些端口。

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

有没有更好的方法可以执行此操作,或者以某种方式锁定“ localhost”的行以暂时允许这些流量进入​​?

Dan*_*rin 6

您有两种选择:

  1. 您可以为此设置一个自引用安全组,这意味着由于该安全组上有EC2实例,因此可以与其进行通信。有一个警告,那就是不要使用嵌入式入口和出口规则的AWS::EC2::SecurityGroup,而是使用AWS::EC2::SecurityGroupEgressAWS::EC2::SecurityGroupIngress独立的,如上所述这里

看起来像:

"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"
     }
Run Code Online (Sandbox Code Playgroud)
  1. 最佳选择:在框中创建2个主机条目(或者最好使用Route53专用托管区域设置dns条目):

    webapp1.com 127.0.0.1

    webapp2.com 127.0.0.1

我不建议通过公共IP与自己进行讨论。可能您甚至会承担费用!(https://aws.amazon.com/ec2/pricing/on-demand/)加上SG的新增维护。