2016-08-22 63 views
3

我正在使用平臺條件來控制在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. 

是否可以評估多個條件來控制資源創建?如果沒有,我可以嘗試嗎?

回答

4

嘗試增加

"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" }]} 
    }, 
+0

謝謝,我會嘗試很快。結構是,如果我在堆棧創建期間從下拉列表中選擇窗口,它將在模板中創建50%的EC2資源。如果我選擇Linux,則是另外50%。我希望能夠選擇兩個部署所有實例,主要用於開發和測試目的。將使用上述允許我這樣做,或者我需要將窗口添加到fn ::或? –