![](/img/trans.png)
[英]What format to use when entering an IP address into an EC2 Security Group rule?
[英]How to use an EC2 ip address in a Security Group inside cloudformation
我对cloudformation有点陌生,并且在参数设置或查找有关如何在我的安全组中引用ec2 IP地址的信息时遇到麻烦。 两者在我的堆栈中定义,我的SG取决于EC2。 这是我的SG:
"3DEXPPLAT": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "3DPLAT SG",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": 80,
"ToPort": 80,
"CidrIp": {"Ref" : "CFAPA"}
}
],
"Tags": [
{
"Key": "Name",
"Value": "STACK_3DEXPSG"
}
]
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "59898995-0f8d-4cbe-b315-c9c68df319b2"
}
},
"DependsOn": [
"CFAPA"
]
}
那么,我应该在SecurityGroupIngress的CidrIp中放置什么以使我的SG使用EC2的专用IP?
在EC2实例上使用Fn::Ref
时,您正在检索其实例ID。 对于您的安全组,您需要IP(使用Fn::GetAtt
),并且需要将其格式化为CIDR块(使用Fn::Sub
添加表示法)。 因此,假设CFAPA
是您的EC2实例,这应该可以工作:
{
"3DEXPPLAT": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "3DPLAT SG",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": 80,
"ToPort": 80,
"CidrIp": {
"Fn::Sub": "${CFAPA.PrivateIp}/32"
}
}
]
}
}
}
笔记:
您不需要指定DependsOn
属性,因为CloudFormation会根据引用找出依赖关系。 这简化了代码。
要找出要在Fn::GetAtt
上使用的属性称为PrivateIp
,您必须参考AWS :: EC2 :: Instance文档的“返回值”部分。 在这里您会注意到Fn::Ref
返回实例ID。
请记住,私有IP可能会更改。 因此,您可能希望将CFAPA
附加到另一个安全组,并根据安全组成员身份创建权限。 不过,这是架构决定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.