Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Delivery Pipeline as Code: AWS CloudFormation and AWS CodePipeline

DZone's Guide to

Delivery Pipeline as Code: AWS CloudFormation and AWS CodePipeline

Making use of infrastructure as code to manage your deployment pipeline is an important DevOps pattern.

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

The assembly line is the heart of any factory. Workers and supervisors are giving their best to ensure a steady flow of raw materials to the end products. The delivery pipeline is an important part of DevOps and the equivalent to the assembly line. A delivery pipeline allows you to define and automate the process of making changes to your systems.

Read on to learn how to combine two tools: infrastructure as code and delivery pipelines. You will gain superpower by making use of this pattern!

Delivery Pipeline as Code

What Is Infrastructure as Code?

You can create your cloud infrastructure by clicking through the Management Console, the web interface offered by AWS. Automating the process of provisioning and updating your infrastructure allows you to increase reliability and decrease effort. Describing the target state of your infrastructure in code and using a tool to transform the current state of your infrastructure into the target state is called infrastructure as code.

Our article Understanding Infrastructure as Code explains the concept of infrastructure as code in more detail.

AWS is offering an infrastructure as code tool called AWS CloudFormation. You can define your infrastructure consisting of virtual machines, networking configuration, databases, and any other AWS service within a template. CloudFormation can use the template containing the target state to create or update your infrastructure.

What Is a Delivery Pipeline?

Beside infrastructure as code, every DevOps toolbox should contain delivery pipelines, as well. Pushing every source code change through an automated pipeline ending with a deployment to your production system is called Continuous Delivery. A delivery pipeline defines the process of deploying changes to production. It consists at least of the following steps:

  • Building and packaging.
  • Testing small units and the whole system.
  • Deploying to production.

Think of a delivery pipeline as the assembly line in a DevOps world. AWS is offering a service allowing you to define and execute delivery pipelines called AWS CodePipeline.

Delivery Pipeline as Code

Your delivery pipeline will become as valuable to your company as an assembly line within a factory. It contains all the knowledge needed to deploy a change to production.

Therefore, creating the delivery pipeline should be reproducible and automated in the same way as creating your infrastructure. The delivery pipeline itself should be defined in code. Again, infrastructure as code is the tool of your choice.

Example: CloudFormation and CodePipeline

The following example shows how to use Infrastructure as Code to create a deployment pipeline. The example uses CodePipeline and CloudFormation to deploy a static website on EC2.

The following snippet contains the definition of a CodePipeline consisting of three stages:

  1. Source: References a GitHub repository. A commit to the repository triggers the deployment pipeline automatically.
  2. Deploy: CodeDeploy is used to deploy a static web application on EC2.
  3. Test: A Lambda function sends HTTP request to the EC2 instance to validate the deployment.
...
CodePipeline:
  DependsOn:
    - EC2Instance
  Type: "AWS::CodePipeline::Pipeline"
  Properties:
    ArtifactStore:
      Location: !Ref ArtifactStore
      Type: S3
    RoleArn: !Sub '${CodePipelineIAMRole.Arn}'
    Stages:
      - Name: Source
        Actions: 
        - Name: Source
          ActionTypeId:
            Category: Source 
            Owner: ThirdParty 
            Version: 1 
            Provider: GitHub
          OutputArtifacts:
          - Name: staticwebsite
          Configuration:
            Owner: !Ref GitHubOwner
            Repo: !Ref GitHubRepo
            Branch: master
            OAuthToken: !Ref GitHubOAuthToken
      - Name: Deploy
        Actions:
        - Name: Deploy
          ActionTypeId:
            Category: Deploy 
            Owner: AWS 
            Version: 1 
            Provider: CodeDeploy
          InputArtifacts:
          - Name: staticwebsite
          Configuration:
            ApplicationName: !Ref Application
            DeploymentGroupName: !Ref DeploymentGroup
      - Name: Test
        Actions:
        - Name: Test
          ActionTypeId:
            Category: Invoke 
            Owner: AWS 
            Version: 1 
            Provider: Lambda
          Configuration:
            FunctionName: !Ref TestLambda
            UserParameters: !Sub 'http://${EC2Instance.PublicDnsName}'
...

Find the whole source code here

Summary

In my opinion, making use of infrastructure as code to manage your deployment pipeline is an important DevOps pattern. AWS CloudFormation and AWS CodePipeline allow you to benefit from an automated, reproducible, and transparent deployment pipeline.

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
infrastructure as code ,devops ,delivery pipeline

Published at DZone with permission of Andreas Wittig, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}