【问题标题】:How to make a list item conditional in Cloud Formation template?如何在 Cloud Formation 模板中使列表项有条件?
【发布时间】:2018-01-21 17:01:02
【问题描述】:

我有以下创建代码管道的云形成模板。管道分为三个阶段:

  Stages:
    -
      Name: "Source"
      Actions:
        -
          Name: "Source"
          ActionTypeId:
            Category: "Source"
            Owner: "ThirdParty"
            Version: "1"
            Provider: "GitHub"
          OutputArtifacts:
            - Name: "MyApp"
          Configuration:
            Owner: !Ref GithubOwner
            Repo: !Ref GithubRepo
            PollForSourceChanges: "true"
            Branch: !Ref GithubBranch
            OAuthToken: !Ref GithubTokenParameter
          RunOrder: 1
    -
      Name: "Run-Unit-Tests"
      Actions:
        -
          InputArtifacts:
            - Name: "MyApp"
          Name: "UnitTests"
          ActionTypeId:
            Category: "Test"
            Owner: "AWS"
            Version: "1"
            Provider: "CodeBuild"
          OutputArtifacts:
            - Name: "MyTests"
          Configuration:
          ProjectName: !Ref CodeBuildName
          RunOrder: 1
    -
      Name: "Deploy-Staging"
      Actions:
        -
          InputArtifacts:
            - Name: "MyApp"
          Name: "Deploy-Staging"
          ActionTypeId:
            Category: "Deploy"
            Owner: "AWS"
            Version: "1"
            Provider: "ElasticBeanstalk"
          Configuration:
            ApplicationName: !Ref BeanstalkApplicationName
            EnvironmentName: !Ref BeanstalkEnvironmentStaging
          RunOrder: 1

我也有一个条件:

IncludeStagingEnv: !Equals [Staging, !Ref CodePipelineEnvironment]

当条件为假时,我想省略代码管道阶段列表中的第 3 项。

我尝试将 !If 与 AWS::NoValue 一起使用,但 NoValue 不是有效的列表项:

Stages:
  - !IF
    - IncludeStagingEnv
    - Name: "Deploy-Staging"
      Actions:
        -
          InputArtifacts:
            - Name: "MyApp"
          Name: "Deploy-Staging"
          ActionTypeId:
            Category: "Deploy"
            Owner: "AWS"
            Version: "1"
            Provider: "ElasticBeanstalk"
          Configuration:
            ApplicationName: !Ref BeanstalkApplicationName
            EnvironmentName: !Ref BeanstalkEnvironmentStaging
            RunOrder: 1
    - AWS::NoValue

IncludeStagingEnv==false时如何省略最后一项?

【问题讨论】:

    标签: amazon-cloudformation aws-codepipeline


    【解决方案1】:

    我的 Cloudfront 分发模板也出现了同样的问题。

    解决方案是使用AWS::NoValue Ref 属性。

    ...
    LambdaFunctionAssociations: 
      Fn::If: 
        - Authentication
        - - EventType: "viewer-request"
            LambdaFunctionARN: "arn:aws:lambda:us-east-1:..."
        - - Ref: "AWS::NoValue"
    ...
    

    如果这适用于所有资源,您应该将条件部分更改为:

    Stages:
      - !If
        - IncludeStagingEnv
        - - Name: "Deploy-Staging"
            Actions:
              - InputArtifacts:
                ...
        - - Ref: "AWS::NoValue"
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      @Fabi755 的回答让我走上了正确的道路,谢谢!

      我正在与同样的LambdaFunctionAssociations 挑战作斗争。我选择了一种稍微不同,稍微好一点的方法,如下所示。我认为更好的是它适用于多个可选列表项。

                LambdaFunctionAssociations:
                - !If
                  - HasOriginResponseFunctionArn
                  - EventType: origin-response
                    LambdaFunctionARN: !Ref OriginResponseFunctionArn
                  - !Ref AWS::NoValue
                - !If
                  - HasViewerRequestFunctionArn
                  - EventType: viewer-request
                    LambdaFunctionARN: !Ref ViewerRequestFunctionArn
                  - !Ref AWS::NoValue
      

      【讨论】:

      • 我需要在 CloudFront 设置中做这件事,并认为这是要走的路,但并不期待必须部署很多次才能确保它做到了。感谢您发布此信息!
      • 正是我想要有条件地设置 LambdaFunctionAssociations。谢谢!
      猜你喜欢
      • 2020-01-27
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多