【问题标题】:In AWS CloudFormation are booleans and strings of booleans interchangeable?在 AWS CloudFormation 中,布尔值和布尔值字符串可以互换吗?
【发布时间】:2019-10-15 13:51:21
【问题描述】:

我对 AWS CloudFormation 如何处理布尔值和布尔值的字符串感到困惑。

例如,就 CloudFormation 而言,'true'true(或 'false'false)在逻辑上是否等效?我在他们的快速入门模板中看到了这两种情况的示例,这让我认为它们是(尽管我还没有找到关于这种或另一种方式的文档)。

例如,在他们的模板quickstart-compliance-common/templates/vpc-production.template 中,他们定义了一个“String”类型的变量pSupportsNatGateway(尽管它的默认值是literaltrue):

Parameters:
  ...
  pSupportsNatGateway:
    Description: Specifies whether this region supports NAT Gateway (this value is
      determined by the main stack if it is invoked from there)
    Type: String
    Default: true

然后,在模板后面的condition 中,将该参数(可能是字符串)与文字true 进行比较。

Conditions:
  ...
  cSupportsNatGateway:
    !Equals
    - true
    - !Ref pSupportsNatGateway

我的问题是,CloudFormation 如何比较文字值和这些值的字符串?这在 AWS 文档中的什么地方定义?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    我不知道这是在哪里记录的,但是是的!就 CloudFormation 而言,文字布尔值(或数字)及其字符串值似乎是等价的。

    我创建了一个最小的 CloudFormation 模板来测试这个:

    ---
    AWSTemplateFormatVersion: 2010-09-09
    Description: Test CloudFormation template
    
    Parameters:
    
      pCreateCluster:
        Description: To create or not create?
        Type: String
        Default: 'true'
        AllowedValues:
        - 'true'
        - 'false'
    
    Conditions:
      CreateClusterConditionTrue1:
        !Equals
        - !Ref pCreateCluster
        - 'true'
    
      CreateClusterConditionTrue2:
        !Equals
        - !Ref pCreateCluster
        - true
    
      CreateClusterConditionFalse1:
        !Equals
        - !Ref pCreateCluster
        - 'false'
    
      CreateClusterConditionFalse2:
        !Equals
        - !Ref pCreateCluster
        - false
    
    Resources:
    
      rFargateCluster:
        Type: AWS::ECS::Cluster
        Condition: CreateClusterConditionTrue1
        Properties:
          ClusterName: "my-test-cluster"
    
    Outputs:
      CreateClusterConditionTrue1:
        Value:
          !If
          - CreateClusterConditionTrue1
          - "The answer is True"
          - "The answer is False"
      CreateClusterConditionTrue2:
        Value:
          !If
          - CreateClusterConditionTrue2
          - "The answer is True"
          - "The answer is False"
      CreateClusterConditionFalse1:
        Value:
          !If
          - CreateClusterConditionFalse1
          - "The answer is True"
          - "The answer is False"
      CreateClusterConditionFalse2:
        Value:
          !If
          - CreateClusterConditionFalse2
          - "The answer is True"
          - "The answer is False"
    ...
    

    结果表明它们实际上是等价的:

    Key                             Value
    CreateClusterConditionTrue1     The answer is True      
    CreateClusterConditionTrue2     The answer is True      
    CreateClusterConditionFalse2    The answer is False     
    CreateClusterConditionFalse1    The answer is False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多