Stelligent Amazon Pollycast
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.
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.
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:
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:
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.
- Clone the app repo
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.
Pulls down the source code artifact from S3 and unzips it.[gist https://gist.github.com/dmbettinger/ad0ad38efa281e7fe93b2af1bfb0d9bb]
Sets Habitat environment variables and generates/uploads a key to access my Origin on the Habitat Depot.[gist https://gist.github.com/dmbettinger/f7deed4b2a33a4c309ac17160031f870]
Runs static analysis on plan.sh using bash -n.[gist https://gist.github.com/dmbettinger/5b55464b0f025f06ffdb8038bb2be987]
Builds the Habitat package[gist https://gist.github.com/dmbettinger/3c38ad05337e0594762ff0b8a0e559fe]
Creates a Docker test environment by running a Habitat package export command inside the Habitat Studio.[gist https://gist.github.com/dmbettinger/3bfa4fc7b6a861bd1196040749920312]
Runs a Bats test suite which verifies that the webserver is running and the “hello world” page is displayed.[gist https://gist.github.com/dmbettinger/504cdf891930fb045a17ffae65ff2a69]
Uploads the Habitat package to the Depot. In a later pipeline stage, a package deployment can be sourced directly from the Depot.[gist https://gist.github.com/dmbettinger/cc2200c4e99f177e6a1263b291424e7e]
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.