如何在 Cloudformation 模板中使列表项成为条件?

2024-03-24

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

  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?


我的 Cloudfront 发行版模板上也出现同样的问题。

解决方案是使用AWS::NoValue with the 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"

希望这可以帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Cloudformation 模板中使列表项成为条件? 的相关文章

随机推荐