With AWS CloudFormation we can configure and define infrastructure as code, this approach allows to provision and update the infrastructure in a consistent way. If the developers need to use test environment that matches our production environment it can be done within minutes using a CloudFormation template. If we need to expand our infrastructure to another AWS region we can also use CloudFormation templates to deploy new infrastructure and maintain its consistency with already existing infrastructure. With CloudFormation we can ensure that our infrastructure is configured to match any compliance requirements. After the successful deployment of your infrastructure we can manage the code definition in the same way as we would manage our application enabling us to version control of our infrastructure.
Using AWS CloudFormation with AWS CodeCommit and CodePipeline we are able to build Continuous Delivery Pipeline for the deployment and update our CloudFormation templates. This allows us to provision and update infrastructure automatically, and carry out any necessary testing or approval before releasing our infrastructure in to production. This reduces build times and approval times, and ensures rapid deployment of our infrastructure and implementations of changes.
We will now use AWS CodeCommit and CodePipeline to build an example pipeline that will deploy web application using CloudFormation template. The CodePipeline detects any changes in files in CodeCommit and automatically fetches the source from our master branch and automatically builds a test stack for review, also we will add an approval step to choose if we want to release the new updated stack in to our production environment.
AWS CodeCommit its highly scalable and secure Git repositories; we’re going to use it to store and maintain our CloudFormation templates and the source of our pipeline. To create a new repository navigate to CodeCommit on AWS Console and select the N.Virginia as a region, and then select ‘Create Repository’. Give the repository a unique name and select create a repository.
We can connect to the new repository using SSH or HTTPS, we’re going to use HTTPS. We need an IAM user with Git Credentials, details for configuring the IAM user are detailed in the CodeCommit User Guide.
Once those pre-reqs are met we can clone the new repository to create a local repo, from the command line we type:
We have now created local repository. We can now add our CloudFormation template to the repo. We will use an example template that will deploy a multi-AZ, load balanced and scalable application. We’re also adding a configuration file that will complete the necessary parameters for the CloudFormation template.
The following commands commit the files to the local repo and push the commit to the AWS CodeCommit repository.
Now we’ve configured our source we can build our pipeline. AWS CodePipeline is a continuous delivery service we can use to automatically build, test and deploy applications and infrastructure into the cloud. Using CodePipeline we can automate the release process and ensure consistent deployments and updates of applications and infrastructure. By delivering infrastructure in this rapid, safe and consistent way we allow our developers to focus on application development, without the long wait times associated with traditional release methodologies.
The first step in building our pipeline is to configure a source. We are going to use the CodeCommit repository we set up, but you could also use AWS S3 or GitHub.
We’re then going to configure a deployment stage, this is where we define how the CloudFormation template is deployed.
At this point, we have a basic pipeline that will detect updates and changes to the CloudFormation template and automatically deploy that CloudFormation template. We can build out our pipeline by adding additional stages and actions. In this example we have added stages to build the CloudFormation template as a test stack then email stakeholders for review of the test stack and approval of deployment. If approved the test stack will be deleted and a CloudFormation Change Set created. We use the Change Set to make sure any existing Production Stacks and resources are not removed without our knowledge or consent.
The pipeline will run anytime it detects a change to the CloudFormation template. We’ll manually release the change to run through the deployment process. First of all, the test stack and instances are created and we’re sent an email prompting us to review and approve the changes, we can see here an EC2 instance has been started and it is hosting our WebApp accessible via our load balancer. If we’re happy with the test infrastructure and the application we can approve the release, at this point the test stack will be deleted and a Change Set will be created for review.
The Change Set details all changes the template will make and its potential impact on existing resources, we’ll be prompted by the pipeline to review and approve those changes. If we’re happy with the changes and approve the Change Set the infrastructure and application will be deployed into Production. Any future updates to the template will be automatically detected, a test environment created for reviewing those changes and then the deployment to production automated as well; and that’s it, we’ve just built a pipeline that repeatedly and reliably provisions and updates our infrastructure allowing our developers to quickly begin work on the development and testing of applications.