AWS:如何在 CloudFormation 模板中指定布尔参数

AWS: How to specify a boolean parameter in a CloudFormation template

我正在尝试在 CloudFormation 模板中指定一个布尔参数,以便我可以根据传入的参数有条件地创建资源。

查看文档 here and here 似乎明显缺少布尔数据类型。

指定布尔值的最佳做法是什么?可能 Number 与 0 或 1 或 StringAllowedValues 'true' 和 'false'?

Quick Start templates are a good, semi-official reference point of how complex templates can/should be created, and they implement Boolean values for Conditional resources exactly as you described, using a String with AllowedValues true and false. Here's a specific example:

"EnableBanner": {
    "AllowedValues": [
        "true",
        "false"
    ],
    "Default": "false",
    "Description": "To include a banner to be displayed when connecting via SSH to the bastion, set this parameter to true",
    "Type": "String"
}

可以在 CloudFormation 文档的 Conditionally use an existing resource 示例中找到类似的示例,其中 AllowedValuesdefaultNONE(默认值)。

要根据此类布尔参数有条件地创建资源,请添加一个 Condition statement containing a Fn::Equals 匹配 true 的内在函数,然后向该资源添加一个 Condition 键。

这是一个完整的最小示例模板:

Parameters:
  CreateResource:
    Description: Whether I should create a resource.
    Default: false
    Type: String
    AllowedValues: [true, false]
Conditions:
  ShouldCreateResource:
    !Equals [true, !Ref CreateResource]
Resources:
  Resource:
    Type: AWS::CloudFormation::WaitConditionHandle
    Condition: ShouldCreateResource