Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negate a Condition in CloudFormation Template

I have the following condition, accordingly to that condition I create some resources, while if that condition is not met then I create other resources.

Conditions:   ISProduction:     "Fn::Equals":       - !Ref Environment       - staging   ISNotProduction:       "Fn::Not":         - !Ref ISProduction 

However, when I try to evaluate the template with the snippet above I get the error:

Template error: every Fn::Not object requires one boolean parameter

How can I negate a condition in a Cloud Formation Template? Or how can I use the negation of ISProduction?

I also tried the Condition below in a resource creation, but I but the template do not pass the validation because "Every Condition member must be a string".

Condition:       "Fn::Not":         - !Ref ISProduction 
like image 618
cloudy_weather Avatar asked Dec 12 '16 17:12

cloudy_weather


People also ask

Which section of a CloudFormation template does not allow for conditions?

According to the docs, Conditions should be used at the top level of the resource you want to conditionally create. Putting a Condition inside the Instance UserData section isn't supported. To use Conditions in your situation, you'd want separate Resources conditionally created based on the Parameter.

What is FN :: if?

Fn::If. Returns one value if the specified condition evaluates to true and another value if the specified condition evaluates to false .

What is pseudo parameter in AWS CloudFormation?

Pseudo parameters are parameters that are predefined by AWS CloudFormation. You don't declare them in your template. Use them the same way as you would a parameter, as the argument for the Ref function.

What is conditions in cloud formation?

With conditions, you can define which resources are created and how they're configured for each environment type. Conditions are evaluated based on predefined pseudo parameters or input parameter values that you specify when you create or update a stack.


Video Answer


1 Answers

You can reference other conditions by using the Condition key before your Condition Logical ID.

Associating a Condition

To conditionally create resources, resource properties, or outputs, you must associate a condition with them. Add the Condition: key and the logical ID of the condition as an attribute to associate a condition, as shown in the following snippet. AWS CloudFormation creates the NewVolume resource only when the CreateProdResources condition evaluates to true.

Your example should look like this:

Conditions:   ISProduction:     "Fn::Equals":       - !Ref Environment       - staging   ISNotProduction:       "Fn::Not":         - Condition: ISProduction 

Optionally you can write it in the short form:

Conditions:   ISProduction:     !Equals [!Ref Environment, staging]   ISNotProduction:     !Not [Condition: ISProduction] 
like image 176
George M Whitaker Avatar answered Sep 23 '22 22:09

George M Whitaker