AWS Velocity Series: EC2 based app CI/CD pipeline
In the previous article, you learned how to use CloudFormation to describe a production-ready infrastructure for an EC2 based app. In this article you will...
In the previous article, you learned how to use CloudFormation to describe a production-ready infrastructure for an EC2 based app. In this article you will learn to:
- Automate the creation of an AMI that contains the app with Packer
- Deploy a CloudFormation stack based
infrastructure/ec2.ymlwith AWS CodePipeline - Run the Acceptance tests on AWS CodeBuild against the infrastructure created in the previous step
- Deploy another CloudFormation stack for the production environment
You can follow step by step or get the full source code here: https://github.com/widdix/aws-velocity
The pipeline is based on the CI/CD Pipeline as Code part of this series.

AWS Velocity Series
Most of our clients use AWS to reduce time-to-market following an agile approach. But AWS is only one part of the solution. In this article series, I show you how we help our clients to improve velocity: the time from idea to production. Discover all posts!
Additionally, the EC2 based app pipeline contains:
- add a BuildAMI action to the Build stage.
- add an Acceptance stage
- add a Production stage
Copy the deploy/pipeline.yml file to deploy/pipeline_ec2.yml to get the starting point right. If you don’t have the deploy/pipeline.yml file you can download it from https://github.com/widdix/aws-velocity.
BuildAMI Action
EC2 instances start from an image (AMI) that contains the operating system including all the files that are needed. You can create your own AMI as well. Usually, you take one of the available AMIs like the Amazon Linux, make your modifications, and then create a new image from that. This whole procedure can also be automated with a tool called Packer. You will now see how you can run Packer in CodeBuild to create a new AMI that contains the app.
Packer itself needs configuration files. Create a file infrastructure/packer.json with the following content that build a new AMI based on ami-c51e3eb6 (Amazon Linux) and a Bash script that you will create later:
{
|
Packer is configured to run a Bash script to provision the AMI and to upload the app folder. Create a file infrastructure/packer.sh with the following content to:
- install the latest patches
- install Node.js 6.x
- install the CloudWatch Logs agent
- install
forever, a tool to run Node.js script in the background
#!/bin/bash -ex |
The script also moves the application files to the right place.
To integrate Packer into the pipeline, add the following resources to the Resources section of deploy/pipeline_ec2.yml to create a CodeBuild project to run Packer with the above configuration. Packer also needs a bunch of IAM permissions which are also added.
# Packer needs a set of access rights as defined in https://www.packer.io/docs/builders/amazon.html |