问题
I'm using a platform condition to control the type of environment that gets spun up on AWS. There are plenty of shared resources, but I need certain EC2 instances with pre-baked AMIs depending on a number conditions.
"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"
},
Then I set the conditions.
"Conditions" : {
"LinuxPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "linux"]},
"WindowsPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "windows"]},
"BothPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "both"]}
},
In a resource I'd like to use either linux or windows to trigger a Windows or Linux Ec2 creation, or use both to deploy every ec2 resource declared.
I've tried the following using fn:or in a few ways.
"Fn::Or": [{"Condition": "LinuxPlatform"}, {"Condition": "BothPlatform" }],
and...
"Condition" : {
"Fn::Or" : [
{"Condition" : "LinuxPlatform"},
{"Condition" : "BothPlatform"}
]
}
I keep getting the following error when trying to deploying and validating using the 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.
Is it possible to evaluate multiple conditions to control resource creation? If not are there any alternatives I could try?
回答1:
Try adding
"MyCondition": {"Fn::Or": [{"Condition": "LinuxPlatform"}, {"Condition": "BothPlatform" }]}
to the bottom of your Conditions like that:
"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" }]}
},
回答2:
I was looking for the same thing with different scenarios in the YAML format. Below is the YAML format for the reference.
CreateResources: !Or [!Equals [!Ref "Environment", prod], !Equals [!Ref "Environment", dev], !Equals [!Ref "Environment", preprod], !Equals [!Ref "Environment", test]]
来源:https://stackoverflow.com/questions/39086361/multiple-conditions-in-cloud-formation-resource-creation