这些问题很可能在问完这个问题之后被添加,但是对于现在碰到这个问题的任何人来说,这可以用CloudFormation中的Conditions完成。
因此,如果我们开始与您的参数声明
"Parameters" : {
"SecurityGroup" : {
"Description" : "Name of an existing EC2 Security Group ",
"Type" : "String",
"Default" : "default",
"MinLength": "1",
"MaxLength": "64",
"AllowedPattern" : "[-_ a-zA-Z0-9]*",
"ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
},
},
我们可以添加一个Conditions
声明,符合条件ShouldCreateSecurityGroup
"Conditions" : {
"ShouldCreateSecurityGroup" : {"Fn::Equals" : [{"Ref" : "SecurityGroup"}, "default"]}
},
这种情况,现在可以用来告诉CloudFormation是否创建安全组:
"Resources": {
"NewSecurityGroup": {
"Type" : "AWS::EC2::SecurityGroup",
"Condition" : "ShouldCreateSecurityGroup"
"Properties" : {
"SecurityGroupEgress" : [ Security Group Rule, ... ],
"SecurityGroupIngress" : [ Security Group Rule, ... ],
}
}
}
然后,当您参考此处的值时,可以使用Fn::If条件函数来说明您是要使用SecurityGroup
参数或NewSecurityGroup
资源中的值。例如,传递值到EC2实例的SecurityGroups
参数,我们可以使用{"Fn::If}
,如:
"Server": {
{
"Type" : "AWS::EC2::Instance",
"Properties" : {
...
"SecurityGroups" : [ {"Fn::If": ["ShouldCreateSecurityGroup", {"Ref": "NewSecurityGroup"}, {"Ref": "SecurityGroup"}]} ],
}
}
}
BTW,如2013年11月的,CFN现在支持条件语句:HTTP://docs.aws.amazon。 COM/AWSCloudFormation /最新/ UserGuide /条件形部structure.html – chris