Multiple conditions in cloud formation resource creation
Solution 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" }]}
},
Solution 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]]
example
---
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS cloudformation template bucket. '
Parameters:
Environment:
Description: Enter the environmet name from allowed values
Type: String
AllowedValues:
- qa
- dev
- prod
- stage
Conditions:
Prod: !Equals [ !Ref Environment, production]
dev: !Equals [ !Ref Environment, dev]
stage: !Equals [ !Ref Environment, stage]
qa: !Equals [ !Ref Environment, qa]
CreateResources: !Or [!Equals [!Ref "Environment", prod], !Equals [!Ref "Environment", dev], !Equals [!Ref "Environment", preprod], !Equals [!Ref "Environment", test]]
Resources:
RenderEngineEFSSG:
Type: AWS::EC2::SecurityGroup
Condition: CreateResources
Properties:
GroupDescription: test SG.
GroupName: !Join [ "-", [ !Ref Environment, sgname ] ]
VpcId: vpc-0e4d5cad992b8d65b
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 2049
ToPort: 2049
CidrIp: 0.0.0.0/0
Description: Ingress Rule for Lambda to access EFS.
SecurityGroupEgress: []
Joe Gardiner
Updated on July 09, 2022Comments
-
Joe Gardiner almost 2 years
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?
-
Joe Gardiner over 7 yearsThanks, I'll try that shortly. The structure is that if I select windows from the drop down during stack creation it will create 50% of the EC2 resources in the template. If I select linux it's the other 50%. I want to be able to select both to have all the instances deployed, mainly for dev and test purposes. Would using the above allow me to do that, or would I need to add windows to the fn::or?
-
Efren almost 6 yearsHow is the proposed addition different to the one in the question?
-
lznt about 5 years@Efren It seems the "Condition" in the error message means the “Condition” under a “Resource”, which must be a SINGLE string.
-
MD. RAKIB HASAN over 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.