否定 CloudFormation 模板中的条件

Negate a Condition in CloudFormation Template

我有以下条件,根据该条件创建一些资源,如果不满足该条件,则创建其他资源。

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

但是,当我尝试使用上面的代码片段评估模板时,出现错误:

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

如何取消云形成模板中的条件? 或者如何使用 ISProduction 的否定?

我在创建资源的时候也尝试了下面的条件,但是模板没有通过验证,因为"Every Condition member must be a string"。

Condition:
      "Fn::Not":
        - !Ref ISProduction

您可以通过在条件逻辑 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.

您的示例应如下所示:

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

您可以选择将其写成缩写形式:

Conditions:
  ISProduction:
    !Equals [!Ref Environment, staging]
  ISNotProduction:
    !Not [Condition: ISProduction]