Continuous Delivery (CD) is a critical component of DevOps that can’t be overlooked. As you make the transition to CD, you need the right mindset, and the right tools. One tool that is indispensable if you host your apps in AWS is CodeDeploy. In this article, we look at why you would want to use a tool like AWS CodeDeploy, and how it doubles down on Continuous Delivery.
No DevOps without Continuous Delivery
DevOps includes two key processes—Continuous Integration, (CI) and Continuous Delivery. In a rush to get on the DevOps bandwagon, some organizations set up a Git repository and a Jenkins server and call it DevOps. While this approach may improve their development to some extent, their releases are still chaotic, because Git and Jenkins do not fully automate the release process.
To complete your transition to DevOps, continuous builds and tests are not enough. You need to go the last mile and continuously deploy, too. Continuous Delivery is the method of releasing an app in short cycles, up to multiple times a day. This requires building and testing code so that it’s release-ready from the start.
Here are the main benefits of CD:
- Shortens time to market: Today, app competition is based on who can bring the most innovative features to market first. Long release cycles cause significant delay in launching new features, and it costs businesses real money.
- Lets user feedback guide development: As you release more frequently, you can get real-world user feedback faster, and work on the right things first.
- Enforces better engineering practices: Getting code release-ready requires developing with stability in mind, and building in QA practices like test-driven development (TDD).
- Reduces risk: Releasing frequently means each release will have a smaller set of new features and updates. This makes for smaller failures as well, and a more resilient app.
- Frees up time to build new features: Automating the deployment process makes it more predictable, and frees your teams to spend more time building new features rather than firefighting deployment issues.
CodeDeploy Brings CD to AWS
When managing infrastructure on the AWS cloud, you have control over your EC2 instances so you can configure your storage, databases, and networking, depending on your workload. Every app runs on multiple EC2 instances. Deploying code to each of them individually can require a lot of time. As your app scales, this can be a bottleneck. Knowing this, AWS launched CodeDeploy way back in 2014.
AWS CodeDeploy deploys application code from AWS S3, GitHub, or BitBucket to EC2 instances or on-prem instances. If you aren’t yet on AWS and instead manage your infrastructure using a private cloud, CodeDeploy can even deploy to your on-premises servers. With hybrid infrastructure becoming the norm for many large implementations, this is a necessary feature for a cloud deployment tool.
CodeDeploy is powerful enough to deploy code across numerous hosts with varying configurations.
Types of Deployments in CodeDeploy
You can deploy your code in two ways with CodeDeploy.
- In-place deployment: With this method, CodeDeploy deploys your code to the same set of EC2 instances by taking them offline, running the scripts to deploy your code, and then taking the instances back online. This method involves some downtime and should be scheduled, which CodeDeploy lets you do. However, it uses lesser EC2 instances than the alternate method.
- Blue-green deployment: AWS recently announced support for blue-green deployment with CodeDeploy. This involves creating two identical production environments, which can both handle production loads equally well. As one environment is running the live application (say, the blue environment), the release is pushed to the alternate green environment, where it is configured, and tested. Once the green environment is stable, all it takes is a switch in the Elastic Load Balancer to route traffic from blue to green. While this option uses more EC2 resources, it eliminates downtime. This is the preferred method for mission-critical apps. Once the deployment is complete on the green environment, you can destroy the blue environment until your next release is ready. This entire process can be automated by CodeDeploy.
Let’s examine how CodeDeploy handles deployments. Here’s what the entire process looks like:
Your First Deployment with CodeDeploy
Before you can start the deployment, you need to create an IAM user (if you don’t have one already) and ensure each of the components (your repositories, EC2 instances, and CodeDeploy) have access to each other.
There are two ways to deploy your app using CodeDeploy—either from the CodeDeploy console or from the AWS CLI. Whichever you choose, you’ll follow these steps to deploy your app:
Step 1. Launch and Configure Your EC2 Instances
This is the first step to decide how many EC2 instances your app needs, and launch them. You can either do this manually, or through an AWS CloudFormation template which makes it easy to create instances quickly. The instances must be tagged or belong to an auto scaling group so that CodeDeploy knows where to deploy your code.
Tags are used by other AWS services to identify instances. You can specify any combination of key-value pair, but be sure to use the correct one in your AppSpec file, or your deployment will not execute.
The instances must have the CodeDeploy agent installed and running inside each of them.
Step 2. Create Your First Revision
At this step, you create an empty root folder on your development machine where you store the text files, binaries, and packages that make up your app. Next, you need to create the AppSpec file in the same folder. You can use a template to include information about your code. You can include file names and their destinations, permissions as needed, and “Hooks” that inform CodeDeploy which scripts to run in what order during a deployment. You bundle all these files to form your first revision.
Step 3. Upload to S3, GitHub, or BitBucket
Once your revision is ready, you upload it to S3, GitHub, or BitBucket. From here, the CodeDeploy agent reads the AppSpec file—and from the “Hooks” section of the AppSpec file, understands which scripts should be run on the EC2 instances.
Step 4. Deploy Your App
CodeDeploy automatically deploys your app every time you upload a revision. It lets you define how you’d like your app to be deployed. You can deploy your app on one instance at a time, on just half the instances first and the second half later, or on all instances at one time. For large clusters, you may want to opt for a phased-out deployment, and for smaller deployments, you can deploy all at once.
Step 5. Monitor your deployment using CloudWatch
Once deployed, you can monitor your deployment using AWS CloudWatch, which integrates with CodeDeploy. Ensure you’ve set the appropriate IAM access permissions for CloudWatch to track CodeDeploy events.
Step 6. Rollback or Redeploy as Needed
If your deployment fails, you can set CodeDeploy to automatically rollback to the previous revision. This is critical to ensuring uptime despite deployment errors. You can then troubleshoot errors, and redeploy the revision when it’s fixed.
AWS and Third-party Integrations
AWS is the most mature public cloud vendor. It has integrations with every possible IT vendor, and cloud services to meet every niche requirement. CodeDeploy is equally well integrated with other DevOps tools.
- CodeDeploy – integrates with other AWS services like:
- CloudWatch – to set up rules for deployments based on events and alarms. CloudWatch also collects log data for deployments.
- CodePipeline – to visually manage your entire development pipeline
- CloudFormation – to create a large number of EC2 instances quickly
- Simple Notification Service – to receive SMS or email notifications for events like failed deployments
It has integrations with third-party tools that manage the workflow across the entire development lifecycle. This includes tools like:
- GitHub and BitBucket, from where it pulls revisions to be deployed
- Continuous Integration tools like Jenkins, CircleCI, and Atlassian Bamboo
- Configuration management systems such as Chef and Puppet
While configuration management tools focus on provisioning and managing instances, CodeDeploy focuses exclusively on deploying code to EC2 instances once created. With these integrations, AWS has made it easy for CodeDeploy to be added to your development stack, no matter what mix of tools you use across your development pipeline.
Monitoring Revisions and Instances
CodeDeploy assigns a health status to every revision and EC2 instance. The revision status can be either “current,” “old,” or “unknown.” A “current” revision is when the revision on the EC2 instance matches the most recent revision in S3, GitHub, or BitBucket. “Old” revisions are when a revision on an instance matches an older revision in S3, GitHub, or BitBucket. “Unknown” is when there isn’t a match.
Similarly, for EC2 instances, CodeDeploy assigns a status of “healthy” or “unhealthy.” If a deployment is successful, the status is healthy, but if a deployment fails, the instance gets a status of “unhealthy.”
Using these checks, you can set rules to deploy revisions only if there are a minimum number of healthy EC2 instances. This ensures your deployment doesn’t break even more instances.
You can set up alerts for your failed deployments using AWS SNS (Simple Notification Service) via CloudWatch alarms. Stopped or failed deployments can be monitored by looking at the CodeDeploy logs. These logs are stored for seven days by default. You could do basic searching and filtering of your logs from inside CloudWatch.
CodeDeploy is an essential tool for AWS dev shops that are making the move to Continuous Delivery. Whether you perform in-place or blue-green deployments, or host your app exclusively in AWS’ public cloud or in a hybrid environment, with the need to integrate with other DevOps tools, CodeDeploy has you covered. It is invaluable to DevOps teams that want to speed up application delivery and not compromise on high availability.