Search code examples
amazon-web-servicesaws-cloudformationaws-cloudformation-custom-resource

Cloudformation If with two Or


I have a CFN template with the following block:

- Fn::If:
   - PreDefinedCondition1
   - !Ref ValueIfTrue
   - !Ref AWS::NoValue

I have a 2nd condition and I want the If function to evaluate either condition using Fn::Or but it keeps failing with a message that If expects a string or condition. This is what I am trying to achieve

- Fn::If:
   - Fn::Or
     - PreDefinedCondition1
     - PreDefinedCondition2
   - !Ref ValueIfTrue
   - !Ref AWS::NoValue

Both PreDefinedCondition1 and PreDefinedCondition2 are already defined under the Conditions: block in the same template.

In simpler language, "if PreDefinedCondition1 !Or PreDefinedCondition2, assign ValueIfTrue" How do I achieve this in Cloudformation?


Solution

  • You have to create new condition in your Conditions section. For example:

    Conditions:
    
        PreDefinedCondition1:
            !Equals [1, 1]
            
        PreDefinedCondition2:
            !Equals [1, 1]   
            
        Cond1OrCond2:
            !Or [Condition: PreDefinedCondition1, Condition: PreDefinedCondition2]  
    

    Then, later you just use Cond1OrCond2:

    - Fn::If:
       - Cond1OrCond2
       - !Ref ValueIfTrue
       - !Ref AWS::NoValue