如何在Cloud Formation模板中使列表项有条件?

时间:2017-08-13 20:29:51

标签: amazon-cloudformation aws-codepipeline

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

  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]

当条件为false时,我想省略Code Pipeline阶段列表中的第3项。

我尝试过使用!如果使用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

时省略最后一项?

2 个答案:

答案 0 :(得分:9)

我的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"

希望这有帮助!

答案 1 :(得分:1)

@ 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