代码之家  ›  专栏  ›  技术社区  ›  Arel user3751769

在AWS cloudformation中,布尔值和布尔值串是否可以互换?

  •  0
  • Arel user3751769  · 技术社区  · 7 年前

    我对AWS CloudFormation如何处理布尔值和 胸部。

    例如,是 'true' true (或) 'false' false )就云形成而言,逻辑上是等价的?我在他们的快速启动模板中看到了这两种情况的例子,这让我觉得它们是(即使我没有找到这种或那种方式的文档)。

    例如,在他们的模板中, quickstart-compliance-common/templates/vpc-production.template 它们定义了一个变量, pSupportsNatGateway ,类型为“string”(即使其默认值为 字面意义的 价值, ):

    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 在模板的后面,将该参数(可能是字符串)与 字面意义的 价值, .

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

    我的问题是,cloudformation如何比较文本值和 这些价值观?AWS文件中对此作了何定义?

    0 回复  |  直到 7 年前
        1
  •  0
  •   Arel user3751769    7 年前

    我不知道这是在哪里记录的,但是 !就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