云形成资源创建的多个条件

时间:2016-08-22 18:28:52

标签: amazon-web-services amazon-ec2 amazon-cloudformation

我正在使用平台条件来控制在AWS上启动的环境类型。有很多共享资源,但我需要某些EC2实例和预先设置的AMI,具体取决于数量条件。

"Parameters": {
"Platform": {
  "Description": "Select platform type - linux or windows",
  "Default": "linux",
  "Type": "String",
  "AllowedValues": [ "linux", "windows", "both" ],
  "ConstraintDescription": "Must enter either linux, windows, or both"
},

然后我设置conditions

"Conditions" : {
  "LinuxPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "linux"]},
  "WindowsPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "windows"]},
  "BothPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "both"]}
},

在资源中,我想使用linux或windows来触发Windows或Linux Ec2创建,或者使用它们来部署所有声明的ec2资源。

我在几个方面使用fn:or尝试了以下内容。

"Fn::Or": [{"Condition": "LinuxPlatform"}, {"Condition": "BothPlatform" }],

和...

"Condition" : {
   "Fn::Or" : [
      {"Condition" : "LinuxPlatform"},
      {"Condition" : "BothPlatform"}
   ]
}

尝试使用aws cli进行部署和验证时,我一直收到以下错误。

aws cloudformation validate-template --template-body       file://./cloudformation/deploy.json

A client error (ValidationError) occurred when calling the ValidateTemplate operation: Template format error: Every Condition member must be a string.

是否可以评估多个条件来控制资源创建?如果没有,我可以尝试其他替代方案吗?

2 个答案:

答案 0 :(得分:8)

尝试添加

"MyCondition": {"Fn::Or": [{"Condition": "LinuxPlatform"}, {"Condition": "BothPlatform" }]}

Conditions的底部,就像那样:

    "Conditions" : {
        "LinuxPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "linux"]},
        "WindowsPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "windows"]},
        "BothPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "both"]},
        "MyCondition": {"Fn::Or": [{"Condition": "LinuxPlatform"}, {"Condition": "BothPlatform" }]}
    },

答案 1 :(得分:0)

我一直在寻找YAML格式的不同场景下的同一件事。 以下是供参考的YAML格式。

CreateResources: !Or [!Equals [!Ref "Environment", prod], !Equals [!Ref "Environment", dev], !Equals [!Ref "Environment", preprod], !Equals [!Ref "Environment", test]]