One-Click Deployment with AWS CodeDeploy

AWS CodeDeploy is a deployment system that enables developers to automate the deployment of applications on EC2 instances  and to update the applications as required.

You can deploy a nearly unlimited variety of application content, such as code, web and configuration files, executables, packages, scripts, multimedia files, and so on. AWS CodeDeploy can deploy application content stored in Amazon S3 buckets, GitHub repositories, or Bitbucket repositories. You do not need to make changes to your existing code before you can use AWS CodeDeploy.

AWS CodeDeploy makes it easier for you to rapidly release new features, helps you avoid downtime during application deployment, and handles the complexity of updating your applications, without many of the risks associated with error-prone manual deployments.

The first step to continuous deployment with CodeDeploy is setting up your EC2 instances, tagging them so you can define deployment groups, installing the CodeDeploy agent on your hosts and setting up trust-roles so that CodeDeploy can communicate with the CodeDeploy agents.

CodeDeploy Application

A CodeDeploy application is a collection of settings about where your application can be deployed, how many instances can be deployed to at once, what should be considered a failed deploy, and information on the trust-role to use to allow CodeDeploy to interact with your EC2 instances.

A CodeDeploy application does not specify what is to be deployed or what to do during the deployment. What to deploy is an archive of code/resources stored in S3 called an application revisionHow to deploy is specified by the AppSpec file located inside the application revision.

The AppSpec file lives in your repo and tells CodeDeploy which files from your application to deploy, where to deploy them, and also allows you specify lifecyle scripts to be run at different stages during the deployment. You can use these lifecycle scripts to stop your service before a new version is deployed, run database migrations, install dependencies etc.

Benefits of AWS CodeDeploy

AWS CodeDeploy offers these benefits:

  • Automated deployments. AWS CodeDeploy fully automates your application deployments across your development, test, and production environments. AWS CodeDeploy scales with your infrastructure so that you can deploy to one instance or thousands.
  • Minimize downtime. AWS CodeDeploy helps maximize your application availability. During an in-place deployment, AWS CodeDeploy performs a rolling update across Amazon EC2 instances. During a blue/green deployment, the latest application revision is installed on replacement instances, and traffic is rerouted to these instances when you choose, either immediately or as soon as you are done testing the new environment.
  • Stop and roll back. You can automatically or manually stop and roll back deployments if there are errors.
  • Centralized control. You can launch and track the status of your deployments through the AWS CodeDeploy console or the AWS CLI. You will receive a report that lists when each application revision was deployed and to which Amazon EC2 instances.
  • Easy to adopt. AWS CodeDeploy is platform-agnostic and works with any application. You can easily reuse your setup code. AWS CodeDeploy can also integrate with your software release process or continuous delivery toolchain.

AWS CodeDeploy provides two deployment type options:

In-place deployment: Instances in a deployment group are taken offline, updated with the latest application revision, and then placed back online as part of the same deployment group.

Blue-green deployment: The instances in a deployment group (the original environment) are replaced by a different set of instances (the replacement environment) using these steps:

  • Instances are provisioned for the replacement environment.
  • The latest application revision is installed on the replacement instances.
  • An optional wait time occurs for activities such as application testing and system verification.
  • Instances in the replacement environment are registered with an Elastic Load Balancing load balancer, causing traffic to be rerouted to them. Instances in the original environment are deregistered and can be terminated or kept running for other uses.

Overview of an In-Place Deployment

Here’s how it works:

  1. First, you create deployable content on your local development machine or similar environment, and then you add anapplication specification file (AppSpec file). The AppSpec file is unique to AWS CodeDeploy; it defines the deployment actions you want AWS CodeDeploy to execute. You bundle your deployable content and the AppSpec file into an archive file, and then upload it to an Amazon S3 bucket or a GitHub repository. This archive file is called an application revision (or simply a revision).
  2. Next, you provide AWS CodeDeploy with information about your deployment, such as which Amazon S3 bucket or GitHub repository to pull the revision from and which set of Amazon EC2 instances to deploy its contents to. AWS CodeDeploy calls a set of Amazon EC2 instances adeployment group. A deployment group contains individually tagged Amazon EC2 instances, Amazon EC2 instances in Auto Scaling groups, or both..
  3. Next, the AWS CodeDeploy agent on each instance polls AWS CodeDeploy to determine what and when to pull from the specified Amazon S3 bucket or GitHub repository.
  4. Finally, the AWS CodeDeploy agent on each instance pulls the target revision from the specified Amazon S3 bucket or GitHub repository and, using the instructions in the AppSpec file, deploys the contents to the instance.

AWS CodeDeploy keeps a record of your deployments so that you can get information such as deployment status, deployment configuration parameters, instance health, and so on.

Overview of a Blue/Green Deployment

A blue/green deployment, in which traffic is rerouted from one set of instances (the original environment) to a different set (the replacement environment), offers a number of advantages over an in-place deployment:

  • An application can be installed and tested on the new instances ahead of time and deployed to production simply by switching traffic to the new servers.
  • Switching back to the most recent version of an application is faster and more reliable because traffic can be routed back to the original instances as long as they have not been terminated. With an in-place deployment, versions must be rolled back by redeploying the previous version of the application.
  • Because the instances provisioned for a blue/green deployment are new, they reflect the most up-to-date server configurations, which helps you avoid the types of problems that sometimes occur on long-running instances.

For a blue/green deployment to take place, you must have one or more Amazon EC2 instances with identifying Amazon EC2 tags or an Auto Scaling group. The instances must meet these additional requirements:

  • Each Amazon EC2 instance must be launched with the correct IAM instance profile attached.
  • The AWS CodeDeploy agent must be installed and running on each instance.

Setup AWS CodeDeploy for our application on github :

Create AWS IAM roles

The first step towards setting up codedeploy is to setup two IAM roles. One for CodeDeploy to talk to EC2 instances and other for EC2 instance to access s3.

Create AWS instance

Next step is to Goto EC2 Instances and launch a new instance. While creating an instance you can choose any instance type but make sure to choose CodeDeploy-EC2 as IAM role in Configure instance.

In Add tags section add a tag with Name as key and Value as codedeploy-demo (You can name the instance as per your need)

Install code deploy

Once the instance is booted up we can install the code deploy agent that instance. Since I used ubuntu AMI to create the EC2 instance, we can install the codedeploy agent using apt-get.

sudo apt-get install python-pip ruby wgetcd /home/ubuntu

Now you need to download the agent as per the region of you instance. we can use the below commands to download and install the codedeploy agent.

wget +x ./installsudo ./install auto

Once it is installed you can verify whether the codedeploy agent is running or not by using the command

sudo service codedeploy-agent status

If the service is inactive, you can start the service using the command:

sudo service codedeploy-agent start


Prepare the application

Next is to add the appspec.yml file to the application, appspec.yml file will have information on what to install on to instances and what lifecycle events to run.

The format for appspec.yml file is

version: 0.0os: linuxfiles:  - source: /index.html    destination: /var/www/html/hooks:  BeforeInstall:    - location: deploy/before_install      timeout: 300      runas: ubuntu  AfterInstall:    - location: deploy/restart_server      timeout: 300      runas: ubuntu

The Before Install hook will be

# deploy/before_install#!/bin/bashsudo rm -f /var/www/html/index.html

and After Install hook will be

# deploy/after_install#!/bin/bashsudo service apache2 restart

Setup CodeDeploy on AWS Console:

  • Now its time to create a deployment. On aws console navigate to AWS CodeDeploy and create a new application.
  • Fill in the name of application and instances using the tagand value
  • choose the deployment configuration,
  • Now add the the IAM role, which we create before as the service role
  • Once the application is created, we can deploy new revision.
  • For the first time, the codedeploy app will ask toconnect to Github.
  • Once the github connection is setup, You can provide the repo name along with github username,
  • Now click on Deploy Now, which will deploy to all the instance configured for the deployment application.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s