Neverending Project Part 3: Infrastructure as Code

A big part of DevOps is “Infrastructure As Code”. In this post I’ll be using AWS Cloudformation to create and provision our infrastructure with the press of a button.

In the previous post we set up Continuous Deployment, so that any changes to our code repository were automatically deployed to our dev environment. But we were still creating the resources – EC2 instances, VPCs, etc. – manually using the console. That is fine as a one-off, but there are many benefits to storing the infrastructure configuration in the codebase.

First, it makes sense to store all the configuration for an environment in one place. It’s self-documenting, and it makes your infrastructure reproducible. For example, at present we don’t have a prod environment set up, but once I have a CloudFormation template that specifies the infrastructure, I can spin up or tear down prod with a single command. Infrastructure as code also means that we can version control the infrastructure alongside our codebase – if we need to roll back to a previous version we can do so easily. You can see the CloudFormation template that defines my infrastructure here.

I can then create the entire stack with:

 aws cloudformation create-stack --template-body dev.cloudformation.template

The next step will be adding unit tests and acceptance tests, which can be added to our deployment pipeline and (eventually) allow us to deploy to production.