One-Click Deployment with AWS CodeDeploy
Contributor - 22 March 2017 - 12min
Contributor - 22 March 2017 - 12min
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.
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 revision. How 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.
AWS CodeDeploy offers these benefits:
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:
Here’s how it works:
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.
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:
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:
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 https://aws-codedeploy-ap-south-1.s3.amazonaws.com/latest/installchmod +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