【发布时间】:2020-02-26 14:25:28
【问题描述】:
我正在设计一个 cloudformation 模板,但我需要在几个安全组之间进行选择
我已经定义了一个参数和两个条件来启用一个或另一个具有相同名称的策略(以维护依赖关系)
但模板不适用于这两个选项,
当参数有 True 选项时,堆栈工作,并且值为 False 时显示以下错误:
Template format error: Unresolved resource dependencies [mySecurityGroup] in the Resources block of the template
这是模板的片段:
Parameters:
KeyName:
Description: EC2 KeyPair
Type: 'AWS::EC2::KeyPair::KeyName'
Reception:
Description: Enable reception
Default: False
Type: String
AllowedValues:
- True
- False
Conditions:
Enable:
!Equals [True, !Ref Reception]
Disable:
!Equals [False, !Ref Reception]
Resources:
myVPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: 'true'
EnableDnsHostnames: 'true'
InstanceTenancy: default
Tags:
- Key: Name
Value: myVPC
mySubNet:
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref myVPC
CidrBlock: 10.0.0.0/24
Tags:
- Key: Name
Value: mySubNet
mySecurityGroup:
Condition: Disable
Type: 'AWS::EC2::SecurityGroup'
Properties:
VpcId: !Ref myVPC
GroupDescription: Security Group for EC2
SecurityGroupIngress:
- IpProtocol: udp
FromPort: 4114
ToPort: 4114
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: mySecurityGroup
mySecurityGroup:
Condition: Enable
Type: 'AWS::EC2::SecurityGroup'
Properties:
VpcId: !Ref myVPC
GroupDescription: Security Group for EC2
SecurityGroupIngress:
- IpProtocol: udp
FromPort: 5683
ToPort: 5683
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: mySecurityGroup
myEC2:
Type: 'AWS::EC2::Instance'
Properties:
KeyName: !Ref KeyName
ImageId: ami-028188d9b49b32a80
InstanceType: t2.nano
NetworkInterfaces:
- SubnetId: !Ref mySubNet
AssociatePublicIpAddress: 'true'
DeviceIndex: 0
GroupSet:
- !Ref mySecurityGroup
Tags:
- Key: Name
Value: myEC2
我不确定哪种方法是正确的
【问题讨论】:
-
什么意思它不起作用,请解释错误或结果。
-
当参数有 True 选项时,堆栈工作,并且值为 False 显示以下错误:``` 模板格式错误:模板资源块中未解决的资源依赖项 [mySecurityGroup] ` ``
-
您不需要创建两个完全不同的安全组,您可以使用相同的安全组 - 但有条件地添加入口规则(资源类型为 AWS::EC2::SecurityGroupIngress)。然后您可以使用 Fn::IF 函数来确定是否添加入口规则。请参阅此页面上的 Fn::If:docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…。我认为您的方法会遇到问题,因为 cloudformation 将首先尝试创建新资源,然后再删除冗余资源 - 造成冲突。
标签: amazon-web-services amazon-ec2 amazon-cloudformation aws-security-group