CloudFormation SecurityGroup 循环参考
CloudFormation SecurityGroup Circular Reference
我正在使用两个需要相互通信的简单 Web 应用程序。在 AWS CloudFormation 中,我有一个模板可以创建一个 EC2 实例并将两个应用程序安装在同一台服务器上(最终我会将它们分开,但现在它们位于同一个 EC2 实例上)。
作为 EC2 实例的一部分,我必须定义要使用的 SecurityGroup。现在我一直在使用默认的,但我想动态构建一个。在组中,我允许 SSH 从我的机器进入,并允许从盒子到它自己的几个端口。
当使用默认组时,事实上我可以将服务器的 public ip 添加到它自己的安全组中,以允许它与自己通信。问题是在 CloudFormation 模板期间,我得到了 SecurityGroup 和 EC2 实例之间的循环引用。该实例需要一个 SecurityGroup 才能启动,并且该组需要包含 EC2 框的 Public IP 规则。
是否有更好的方法来执行此操作,或者以某种方式锁定 "localhost" 中的内容以暂时允许这些流量进入?
您有两个选择:
- 您可以为此设置一个自引用安全组,这意味着 EC2 实例将被允许与其自身通信,因为它位于该安全组中。有一个警告,那就是不要在
AWS::EC2::SecurityGroup
中使用嵌入式入口和出口规则,而是单独使用 AWS::EC2::SecurityGroupEgress
和 AWS::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"
}
最佳选择:
在框中创建 2 个主机条目(或者更好的是使用 Route53 private hosted zone 设置 dns 条目):
webapp1.com127.0.0.1
webapp2.com127.0.0.1
我不建议让盒子通过其 public IP 与自己对话。可能您甚至会承担费用! (https://aws.amazon.com/ec2/pricing/on-demand/)
加上 SG 的额外维护。
我们可以在创建过程中自行引用一个安全组:
---
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
请阅读我的 post 了解更多信息:
https://dev.to/anupamncsu/self-referencing-security-groups-on-aws-59gb
我正在使用两个需要相互通信的简单 Web 应用程序。在 AWS CloudFormation 中,我有一个模板可以创建一个 EC2 实例并将两个应用程序安装在同一台服务器上(最终我会将它们分开,但现在它们位于同一个 EC2 实例上)。
作为 EC2 实例的一部分,我必须定义要使用的 SecurityGroup。现在我一直在使用默认的,但我想动态构建一个。在组中,我允许 SSH 从我的机器进入,并允许从盒子到它自己的几个端口。
当使用默认组时,事实上我可以将服务器的 public ip 添加到它自己的安全组中,以允许它与自己通信。问题是在 CloudFormation 模板期间,我得到了 SecurityGroup 和 EC2 实例之间的循环引用。该实例需要一个 SecurityGroup 才能启动,并且该组需要包含 EC2 框的 Public IP 规则。
是否有更好的方法来执行此操作,或者以某种方式锁定 "localhost" 中的内容以暂时允许这些流量进入?
您有两个选择:
- 您可以为此设置一个自引用安全组,这意味着 EC2 实例将被允许与其自身通信,因为它位于该安全组中。有一个警告,那就是不要在
AWS::EC2::SecurityGroup
中使用嵌入式入口和出口规则,而是单独使用AWS::EC2::SecurityGroupEgress
和AWS::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"
}
最佳选择: 在框中创建 2 个主机条目(或者更好的是使用 Route53 private hosted zone 设置 dns 条目):
webapp1.com127.0.0.1
webapp2.com127.0.0.1
我不建议让盒子通过其 public IP 与自己对话。可能您甚至会承担费用! (https://aws.amazon.com/ec2/pricing/on-demand/) 加上 SG 的额外维护。
我们可以在创建过程中自行引用一个安全组:
---
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
请阅读我的 post 了解更多信息: https://dev.to/anupamncsu/self-referencing-security-groups-on-aws-59gb