21

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

  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]

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

我尝试将 !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

4

2 回答 2

21

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

希望这可以帮助!

于 2018-04-24T11:04:59.457 回答
17

@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
于 2019-06-03T04:54:19.277 回答