Back in October I posted the Services Stack. The Services Stack is a collection of CloudFormation templates, to create a microservices application on AWS. If you haven’t seen it already please go check it out.
After posting the Services Stack I got a lot of great feedback, mostly all good ;). Turns out what was missing was Continuous Integration and Continuous Deployment (CI/CD).
I’ve gone back and added these features and am happy to share them with you today. And without further delay, here is the code…
thestackshack/services-stack-cicd_services-stack-cicd - ECS microservices stack with built-in CI/CD_github.com
Everything is a pipeline
I use CodePipeline to continuously update all the CloudFormation stacks, build the code, and deploy the code. There is a pipeline for the application
and one for each service
.
Take a look at this diagram and then we’ll go into more details below:
The application pipeline creates the application specific stacks (i.e. nothing service related) and then bootstraps each of the service pipelines.
The application pipeline is initially created via the command line. It then executes every time a commit is pushed.
The application pipeline consists of the following stages:
Notifications Stage
The notifications stage contains two actions. The first action creates a CloudFormation stack with resources to notify the user about DevOps events like build status, deployment status, etc…The second action executes a CodeBuild to test and deploy a Lambda function within the previous stack, always keeping it fresh.
VPC Stage
The VPC stage contains one action. A CloudFormation stack that sets up a VPC for our application.
ECS Stage
The ECS stage contains one action. A CloudFormation stack that sets up an ECS Cluster with EC2’s and an ALB.
Services Stage
The Services stage contains an action for each microservice. Each action is a CloudFormation stack that creates the pipeline for that service.
The service pipeline creates the service specific stack, performs the code build, and finally the code deployment.
Each service pipeline is executed on every code push to that specific service.
The service pipeline consists of the following stages:
ECR & CodeBuild Stage
The ECR & CodeBuild stage has two actions.
The ECR action simply creates an ECR repository for our Docker images.
The CodeBuild action first builds the code, then pushes the image to ECR.
Service Stage
The Service stage has a single action which is a CloudFormation stack for all the service related resources. The newly created ECR image from the previous stage is used when creating the ECR task, thereby deploying the latest changes. A specific version can be used if one wishes to deploy manually.
Two environments are supported, sandbox and production. Sandbox is triggered off the `develop` branch and production is triggered off the `master` branch.
Infrastructure is just as important as Code. Putting both through CI/CD is the best practice. I hope you like this stack. Please clap if you do (or don’t).