Search code examples
amazon-web-servicesyamlaws-cloudformation

AWS CloudFormation YAML !Or function


Error: "Template validation error: Template format error: Conditions can only be boolean operations on parameters and other conditions"

Working JSON conditions block:

"Conditions" : {
    "CreateBetaResources" : {"Fn::Or" : [ {"Fn::Equals" : [{"Ref" : "Environment"}, "beta"]}, {"Fn::Equals" : [{"Ref" : "Environment"}, "eubeta"]} ]},
    "CreateStagingResources" : {"Fn::Equals" : [{"Ref" : "Environment"}, "staging"]},
    "CreateProdResources" : { "Fn::Or": [ {"Fn::Equals" : [{"Ref" : "Environment"}, "prod"]}, {"Fn::Equals" : [{"Ref" : "Environment"}, "euprod"]} ] }
  },

YAML block which isn't working:

Conditions:
  CreateBetaResources:
    !Or [!Equals [!Ref "Environment", beta], !Equals [!Ref "Environment", eubeta]]
  CreateStagingResources:
    - !Equals [!Ref "Environment", staging]
  CreateProdResources:
    !Or [!Equals [!Ref "Environment", prod], !Equals [!Ref "Environment", euprod]]

Why is this error happening? I've scoured the documentation on "Fn::Or" and conditionals... It seems as though the syntax is correct. I've also tried many, many other formats, but this is the one closest to the documentation example.


Solution

  • The proper way to instantiate !Or/!Equals inside a condition block with YAML is as follows:

    Conditions:
     CreateBetaResources: !Or [!Equals [!Ref "Environment", beta], !Equals [!Ref "Environment", eubeta]]
     CreateStagingResources: !Equals [!Ref "Environment", staging]
     CreateProdResources: !Or [!Equals [!Ref "Environment", prod], !Equals [!Ref "Environment", euprod]]
    

    Do not include the list identifier before calling the !Equals function (-).