Tim W.
Tim W.

Reputation: 884

How to make a list item conditional in Cloud Formation template?

I have the following cloud formation template that creates a code pipeline. The pipeline has three stages:

  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

I also have a condition:

IncludeStagingEnv: !Equals [Staging, !Ref CodePipelineEnvironment]

When the condition is false, I would like to omit the 3rd item in the Code Pipeline stages list.

I tried using !If with AWS::NoValue, but NoValue is not a valid list item:

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

How can I omit the last item when IncludeStagingEnv==false?

Upvotes: 32

Views: 18317

Answers (2)

Fabi755
Fabi755

Reputation: 1514

Same problem occurs on my template for a Cloudfront distribution.

The solution was to use AWS::NoValue with the Ref attribute.

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

If this work for all resources same, you should change your conditional part to:

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

Hope this helps!

Upvotes: 28

Aaron
Aaron

Reputation: 627

@Fabi755's answer put me on the right path thank you!

I was fighting with the same LambdaFunctionAssociations challenge. I settled on a slightly different, slightly better approach as follows. I think it is better in that it works for multiple optional list items.

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

Upvotes: 21

Related Questions