<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: omuthu</title>
    <description>The latest articles on DEV Community by omuthu (@omuthu).</description>
    <link>https://dev.to/omuthu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F731902%2Fb67b3f51-f6d8-41cf-8189-14a4c458f35b.jpg</url>
      <title>DEV Community: omuthu</title>
      <link>https://dev.to/omuthu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omuthu"/>
    <language>en</language>
    <item>
      <title>Dynamic Pipeline Orchestration using CodePipeline, CodeBuild and Step Functions</title>
      <dc:creator>omuthu</dc:creator>
      <pubDate>Thu, 25 Nov 2021 01:31:38 +0000</pubDate>
      <link>https://dev.to/aws-builders/dynamic-build-orchestration-using-codepipeline-codebuild-and-step-functions-2kpa</link>
      <guid>https://dev.to/aws-builders/dynamic-build-orchestration-using-codepipeline-codebuild-and-step-functions-2kpa</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;AWS CodePipeline helps in orchestrating CI/CD pipeline all the way from building and testing to deploying applications, from development to production environments.&lt;/p&gt;

&lt;p&gt;However, one of the limitations of AWS CodePipeline is its inability to dynamically execute or skip any pipeline action.&lt;/p&gt;

&lt;p&gt;This post demonstrates how to setup a CI/CD pipeline to dynamically orchestrate and execute actions based on inputs&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS Services
&lt;/h2&gt;

&lt;p&gt;This solution uses the following AWS services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS CodeCommit – Source Control&lt;/li&gt;
&lt;li&gt;AWS CodeBuild – Build System&lt;/li&gt;
&lt;li&gt;AWS CodePipeline – Continuous Delivery&lt;/li&gt;
&lt;li&gt;AWS Step Functions - Workflow automation using state machines&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Solution Overview
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;A change or commit to the code in the CodeCommit application repository triggers CodePipeline with the help of a CloudWatch event.&lt;/li&gt;
&lt;li&gt;The pipeline downloads the code from the CodeCommit repository, initiates the first build using CodeBuild and securely saves the built artifact to an S3 bucket.&lt;/li&gt;
&lt;li&gt;Once build succeeds, pipeline triggers a Step Function Workflow&lt;/li&gt;
&lt;li&gt;Step Function workflow uses artifact from previous build as input and iterates for each item in the input&lt;/li&gt;
&lt;li&gt;For each input, it triggers respective CodeBuild or Pass state using Choice state &lt;/li&gt;
&lt;li&gt;Step Functions will succeed once all invoked builds suceed&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwai5c6w2hit0peiil8sp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwai5c6w2hit0peiil8sp.png" alt="CodePipeline CodeBuild StepFunctions Orchestration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  CodeCommit
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Created a repo and have a file "sample_source.json" with below content &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;{"services":["service_A", "service_B"]}&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  CodeBuild
&lt;/h3&gt;

&lt;h4&gt;
  
  
  First CodeBuild
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;It copies &lt;a href="https://github.com/omuthu-personal/codepipeline-stepfunction-integrations/blob/master/source/sample_source.json" rel="noopener noreferrer"&gt;source file&lt;/a&gt; and generates ABC.json which is passed as output artifact and consumed by next Step Function&lt;/li&gt;
&lt;li&gt;This is optional when codebuild can generate a similar json file with list of services to be passed as input to step functions&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/omuthu-personal/codepipeline-stepfunction-integrations/blob/master/codebuild/first-codebuild-buildspec.yaml" rel="noopener noreferrer"&gt;BuildSpec&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  ServiceA and ServiceB CodeBuild
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;These are the actual builds invoked by step functions&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/omuthu-personal/codepipeline-stepfunction-integrations/blob/master/codebuild/serviceA-buildspec.yaml" rel="noopener noreferrer"&gt;ServiceA BuildSpec&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/omuthu-personal/codepipeline-stepfunction-integrations/blob/master/codebuild/serviceB-buildspec.yaml" rel="noopener noreferrer"&gt;ServiceB BuildSpec&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step Functions
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fccdl114nf8ae4paydohx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fccdl114nf8ae4paydohx.png" alt="State Machine"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/omuthu-personal/codepipeline-stepfunction-integrations/blob/master/stepfunctions/state-machine.json" rel="noopener noreferrer"&gt;State Machine json&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The artifact passed as json file will be received as json input by step functions state machine&lt;/li&gt;
&lt;li&gt;It receives the input using a "Map" state which supprts iteration&lt;/li&gt;
&lt;li&gt;Within iterator, "Choice" state is used to validate the values and invoke respective "Task" or "Pass" state &lt;/li&gt;
&lt;li&gt;The "Task" state in our scenario will invoke a CodeBuild task using "Optimized Integrations"&lt;/li&gt;
&lt;li&gt;CodeBuild task supports only sync invocation which means the next state following CodeBuild execution has to wait until it finishes.&lt;/li&gt;
&lt;li&gt;However, since the iterator uses a "Choice" state, each iteration is independent and happens in parallel&lt;/li&gt;
&lt;li&gt;In this example, for "ServiceA" and "ServiceB", CodeBuild is executed and "ServiceC" is set as a "pass" state&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Note: Ensure enough permissions are granted for StepFunctions IAM role to make relevant service invocations
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/step-functions/latest/dg/codebuild-iam.html" rel="noopener noreferrer"&gt;StepFunction IAM permissions for CodeBuild&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  CodePipeline
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr44f3ly3b1wjzn4uz687.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr44f3ly3b1wjzn4uz687.png" alt="CodePipeline Definition"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/omuthu-personal/codepipeline-stepfunction-integrations/blob/master/codepipeline/codepipeline-definition.json" rel="noopener noreferrer"&gt;CodePipeline Definition&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once all the above steps are done, all we need to do is to create pipeline in CodePipeline&lt;/li&gt;
&lt;li&gt;Create Source stage using any SCM, I used CodeCommit&lt;/li&gt;
&lt;li&gt;Create a Build stage using existing "First Codebuild" build project&lt;/li&gt;
&lt;li&gt;Create next "Step Sunctions" stage with following params

&lt;ul&gt;
&lt;li&gt;Action Provider: Step Functions&lt;/li&gt;
&lt;li&gt;Input artifacts: Output Artifact from previous "First CodeBuild"&lt;/li&gt;
&lt;li&gt;State machine ARN: Choose the recently created stateMachine ARN&lt;/li&gt;
&lt;li&gt;Input type: File Path&lt;/li&gt;
&lt;li&gt;Input: ABC.json (use whatever file is sent in output artifact from previous build)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Execution
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Release a change to test the pipeline&lt;/li&gt;
&lt;li&gt;In this example, if services list changes in "sample_source.json", step functions will execute only those states&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This solution demonstrated the invocation of Step Functions from CodePipeline and Step Functions execution executing builds in CodeBuild dynamically based upon the received input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://github.com/omuthu-personal/codepipeline-stepfunction-integrations" rel="noopener noreferrer"&gt;Code Repository&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/omuthu/" rel="noopener noreferrer"&gt;My LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>stepfunctions</category>
      <category>codebuild</category>
    </item>
  </channel>
</rss>
