Search code examples
amazon-web-servicesaws-cloudformation

AWS CLI cloudformation validate-template success but getting error during create-stack


Good day. I made changes in the Condition section wherein

  1. I have Con1 as the 1st condition
  2. Then Con2 as the 2nd that is using the Con1

In aws cloudformation validate-template the result is success. But when I start using the create-stack via aws cloudformation create-stack, then I get the error:

An error occurred (ValidationError) when calling the CreateStack operation: Template error: Fn::If cannot be preprocessed

This is the changes I made in the Condition section.

  "Conditions": {
    "Con1": {
      "Fn::Not": [{
        "Fn::Equals": [
          {"Ref": "Environment"},
          "abc"
        ]
      }]
    },
    "Con2": {
      "Fn::Not": [{
        "Fn::Equals": [
          { "Fn::If": 
            [
              "Con1", 
              { "Fn::FindInMap" : [ "iamRoles", {"Ref": "HostnameAppId"},  "abc"] },
              { "Fn::FindInMap" : [ "iamRoles", {"Ref": "HostnameAppId"},  "xyz"] }
            ]
          },
          ""]
        }]
      }
  },


Solution

  • Sadly, Fn::If is not supported in Conditions. From docs:

    Currently, AWS CloudFormation supports the Fn::If intrinsic function in the metadata attribute, update policy attribute, and property values in the Resources section and Outputs sections of a template.