【问题标题】:If else condition cloudformationIf else 条件云的形成
【发布时间】:2021-06-30 23:33:37
【问题描述】:

我想根据输入参数在 cloudformation 中填充一个值。我想将Name 分配为test-svc.abc.comsvc.abc.com,具体取决于环境名称是否为prod。如果环境名称为prod,则该值应为svc.abc.com,否则应始终为{env-name}-svc.abc.com

我有以下表达:

Name: !Join [ '-', [ !Ref EnvironmentName, !Ref 'HostedZoneName' ] ]

在上面的表达式中,HostedZoneName 将作为svc.abc.com 传递,EnvironmentName 的值可以是test, release or prod。所以条件应该被评估为:

Inputs: HostedZoneName -> svc.abc.com, EnvironmentName -> test
Output: test-svc.abc.com

Inputs: HostedZoneName -> svc.abc.com, EnvironmentName -> release
Output: release-svc.abc.com

Inputs: HostedZoneName -> svc.abc.com, EnvironmentName -> 1234567
Output: 1234567-svc.abc.com

Inputs: HostedZoneName -> svc.abc.com, EnvironmentName -> prod
Output: svc.abc.com

它基本上是三元运算符。

Name = EnvironmentName.equals("prod") ? HostedZoneName : EnvironmentName + "-" + HostedZoneName

在 CloudFormation 中遇到 if else 条件。

【问题讨论】:

    标签: amazon-web-services if-statement amazon-cloudformation


    【解决方案1】:

    看看Cloudformation Conditions。您可以使用它们来定义 if 语句,使用 Fn::If

    然后您可以在“资源”部分中使用此条件来定义如何构建您的HostedZoneName

    这是一个例子。您可能需要执行以下操作:

    ...
     "Conditions" : {
        "CreateProdResources" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "prod"]}
      },
    
    ...
    
    "Properties" : {
        "HostedZoneName" : {
          "Fn::If" : [
            "CreateProdResources",
            "svn.abc.com",
            {"Fn::Sub": "${Environment}-svc.abc.com"}
          ]}
      },
    

    【讨论】:

      【解决方案2】:

      根据@rdas 发布的答案,我以 YAML 格式实现了以下表达式:

      ...
      Conditions: 
        IsProductionEnvironment: !Equals [ !Ref EnvironmentName, prod ]
      ...
      
      ...
      Name: !If [IsProductionEnvironment, !Ref 'HostedZoneName', !Join [ '-', [ !Ref EnvironmentName, !Ref 'HostedZoneName' ] ]]
      ...
      

      【讨论】:

        【解决方案3】:

        您可以通过在 !if 中使用 !sub 来实现此目的。下面是我在寻找非生产环境的域前缀 (dev,qa,stage) 的示例。

        您的非产品存储桶名称将是

        dev.mydomain.xyz.com
        qa.mydomain.xyz.com
        stage.mydomain.xyz.com
        

        prod 存储桶名称将是

        mydomain.xyz.com
        

        clouformation Yaml exm

        AWSTemplateFormatVersion: 2010-09-09
        Description: 'AWS cloudformation template for admin panel s3 bucket. '
        Parameters:
          WebBucketName:
            Description: Enter the name of the application
            Type: String
            Default: mydomain.xyz.com
          Environment:
            Description: Enter the environmet name from allowed values
            Type: String
            AllowedValues:
              - qa
              - dev
              - prod
              - stage
        Conditions:
          CreateProdResources: !Equals [!Ref Environment, prod]
          CreatedevResources: !Equals [!Ref Environment, dev]
          CreateqaResources: !Equals [!Ref Environment, qa]
          CreatestageResources: !Equals [!Ref Environment, stage]
          MultiCondition: !Or
            - !Condition CreatedevResources
            - !Condition CreateqaResources
            - !Condition CreatestageResources
        
        Resources:
          WebS3AdminPanel:
            Type: AWS::S3::Bucket
            Properties:
              BucketName:
                !If [MultiCondition, !Sub "${Environment}.${WebBucketName}", !Sub "${WebBucketName}" ]
              Tags:
                - Key: Name
                  Value: test
              WebsiteConfiguration:
                IndexDocument: index.html
                ErrorDocument: error.html
              AccessControl: PublicRead
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-04
          • 2019-07-01
          • 1970-01-01
          • 2011-11-06
          • 2020-09-15
          • 1970-01-01
          相关资源
          最近更新 更多