Automating Habitat with AWS CodePipeline
This article outlines a proof-of-concept (POC) for automating Habitat operations from AWS CodePipeline. Habitat is Chef’s new application automation platform that provides a packaging system that results in apps that are “immutable and atomically deployed, with self-organizing peer relationships.” Habitat is an innovative technology for packaging applications, but a Continuous Delivery pipeline is still required to automate deployments. For this exercise I’ve opted to build a lightweight pipeline using CodePipeline and Lambda.
An in-depth analysis of how to use Habitat is beyond the scope for this post, but you can get a good introduction by following their tutorial. This POC essentially builds a CD pipeline to automate the steps described in the tutorial, and builds the same demo app (mytutorialapp). It covers the “pre-artifact” stages of the pipeline (Source, Commit, Acceptance), but keep an eye out for a future post which will flesh out the rest.
Also be sure to read the article “Continuous deployment with Habitat” which provides a good overview of how the developers of Habitat intend it to be used in a pipeline, including links to some repos to help implement that vision using Chef Automate.
Technology Overview
Application
The application we’re automating is called mytutorialapp. It is a simple “hello world” web app that runs on nginx. The application code can be found in the hab-demo repository.
Pipeline
The pipeline is provisioned by a CloudFormation stack and implemented with CodePipeline. The pipeline uses a Lambda function as an Action executor. This Lambda function delegates command execution to an EC2 instance via an SSM Run Command: aws:runShellScript. The pipeline code can be found in the hab-demo-pipeline repository. Here is a simplified diagram of the execution mechanics:
Stack
The CloudFormation stack that provisions the pipeline also creates several supporting resources. Check out the pipeline.json template for details, but here is a screenshot to show what’s included:
Pipeline Stages
Here’s an overview of the pipeline structure. For the purpose of this article I’ve only implemented the Source, Commit, and Acceptance stages. This portion of the pipeline will get the source code from a git repo, build a Habitat package, build a Docker test environment, deploy the Habitat package to the test environment, run tests on it and then publish it to the Habitat Depot. All downstream pipeline stages can then source the package from the Depot.
- Source
- Clone the app repo
- Commit
- Stage-SourceCode
- Initialize-Habitat
- Test-StaticAnalysis
- Build-HabitatPackage
- Acceptance
- Create-TestEnvironment
- Test-HabitatPackage
- Publish-HabitatPackage
Action Details
Here are the details for the various pipeline actions. These action implementations are defined in a “pipeline-runner” Lambda function and invoked by CodePipeline. Upon invocation, the scripts are executed on an EC2 box that gets provisioned at the same time as the code pipeline.
Commit Stage
Stage-SourceCode
Pulls down the source code artifact from S3 and unzips it.
Initialize-Habitat
Sets Habitat environment variables and generates/uploads a key to access my Origin on the Habitat Depot.
Test-StacticAnalysis
Runs static analysis on plan.sh using bash -n.
Build-HabitatPackage
Builds the Habitat package
Acceptance Stage
Build-TestEnvironment
Creates a Docker test environment by running a Habitat package export command inside the Habitat Studio.
Test-HabitatPackage
Runs a Bats test suite which verifies that the webserver is running and the “hello world” page is displayed.
Publish-HabitatPackage
Uploads the Habitat package to the Depot. In a later pipeline stage, a package deployment can be sourced directly from the Depot.
Wrapping up
This post provided an early look at a mechanism for automating Habitat deployments from AWS CodePipeline. There is still a lot of work to be done on this POC project so keep an eye out for later posts that describe the mechanics of the rest of the pipeline.
Do you love Chef and Habitat? Do you love AWS? Do you love automating software development workflows to create CI/CD pipelines? If you answered “Yes!” to any of these questions then you should come work at Stelligent. Check out our Careers page to learn more.
Stelligent Amazon Pollycast
|