Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally create CodePipeline actions based on CloudFormation conditions

Enable / disable sections of a CloudFormation for CodePipeline using Conditionals:

This creates a manual notification action once staging has been built and passed Runscope tests:

- InputArtifacts: []
      Name: !Join ["",[!Ref GitHubRepository, "-prd-approval"]]
      ActionTypeId:
        Category: Approval
        Owner: AWS
        Version: '1'
        Provider: Manual
      OutputArtifacts: []
      Configuration:
        NotificationArn: !GetAtt ["SNSApprovalNotification", "Outputs.SNSTopicArn"]
        ExternalEntityLink: OutputTestUrl
      RunOrder: 3

How to enable/disable this like other CloudFormation resources with a Condition: .

Action steps don't recognize Condition: param

I could make 2 copies of the whole pipeline code one with and one without and then toggle which pipeline I create but it seems like there should be a better way.

like image 911
Eric Nord Avatar asked Jan 19 '17 21:01

Eric Nord


People also ask

Which section of CloudFormation does not allow for conditions?

According to the docs, Conditions should be used at the top level of the resource you want to conditionally create. Putting a Condition inside the Instance UserData section isn't supported. To use Conditions in your situation, you'd want separate Resources conditionally created based on the Parameter.

What is FN :: if?

Fn::If. Returns one value if the specified condition evaluates to true and another value if the specified condition evaluates to false .


1 Answers

You should be able to accomplish this by conditionally inserting the AWS::CodePipeline::Pipeline Resource's Action into the Actions list using the Fn::If Intrinsic Function referencing your Conditions element, returning the Action when the Condition is true and AWS::NoValue (which removes the property, in this case removing the item from the list) when it is not true:

- !If
  - IsProdCondition
  - InputArtifacts: []
    Name: !Join ["",[!Ref GitHubRepository, "-prd-approval"]]
    ActionTypeId:
      Category: Approval
      Owner: AWS
      Version: '1'
      Provider: Manual
    OutputArtifacts: []
    Configuration:
      NotificationArn: !GetAtt ["SNSApprovalNotification", "Outputs.SNSTopicArn"]
      ExternalEntityLink: OutputTestUrl
    RunOrder: 3
  - !Ref AWS::NoValue
like image 115
wjordan Avatar answered Oct 23 '22 07:10

wjordan