AWS CodeDeploy
Posted By : Ankit Arora | 29-Dec-2016
AWS CodeDeploy for deploying code.
There are many tools used for deployment but today we are going to discuss about
AWS CodeDeploy. A tool designed by AWS for AWS EC2 and on-premesis servers.
Aims of AWS CodeDeploy:
- Automating Deployment
- Centralized control
- Lesser downtime
- Easy to adopt
We can deploy Application code from Amazon S3 or GitHub using CodeDeploy.
We will deploy using AWS S3.
Limitations:
- Can’t use CodeDeploy for existing instances without IAM roles
for CodeDeploy. We have to relaunch instances with IAM roles.
Pre-requisites
Before starting, we need 2 IAM roles:-
- Service Role
- Instance Profile Role.
Service Role: We need to grant service roles to AWS CodeDeploy to read through
EC2 instance tags. For example, for your auto-scaled instances, Service Role Policy will be:
"Version": "2016-10-17",
"Statement": [
{
"Action": [
"autoscaling:PutLifecycleHook",
"autoscaling:DeleteLifecycleHook",
"autoscaling:CompleteLifecycleAction",
"autoscaling:DescribeAutoscalingGroups",
"autoscaling:PutInstanceInStandby",
"autoscaling:PutInstanceInService",
"ec2:Describe*"
],
"Effect": "Allow",
"Resource": "*"
}
After creating IAM role, we will establish trusted relationship with AWS CodeDeploy.
Modify Trust policy like below mentioned policy:-
{
"Version": "2016-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": [
"codedeploy.us-east-1.amazonaws.com",
"codedeploy.us-west-2.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
Instance Profile Role: Launching EC2 instance with proper permissions to access
zip files/code from S3 bucket.
Like:
{
"Version": "2016-10-17",
"Statement": [
{
"Action": [
"s3:Get*",
"s3:List*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
Build Deployment Revision
Firstly, We need to create a compresses archive file with
“appspec(Application Specification)” file & code of application. Appspec is
written in YAML. Appspec gives us power to deploy code on different
eployment targets. Appspec file should be placed in root of source code’s folder.
AppSpec file High level Structure(Example):
version: 0.0
os: linux
files:
- source: /production/
destination: /home/ubuntu/production
hooks:
AfterInstall:
- location: scripts/after.sh
runas: root
BeforeInstall:
- location: scripts/before.sh
runas: root
In this structure,
Version: version of our application
OS: OS of our deployment targets, i.e.: Windows/Linux
Files: files to copy to deployment target.
Hooks: this contains the scripts to run at specific deployment lifecycle.
Available event hooks:-
- ApplicationStop: Performs events when application is stopped
- DownloadBundle: CodeDeploy agent downloads bundle from S3 bucket
- BeforeInstall: Here AWSCodeDeploy starts deployment of code to
deployment targets - Install: Files are copied to deployment targets
- AfterInstall: Installed on deployment targets
- ApplicationStart: Starts before application revision.
- ValidateService: Takes place after validation of services.
Note: ApplicationStop, DownloadBundle, ApplicationStart and ValidateService are
hooks which takes place automatically from CodeDeploy’s end. Our main focus is to
create script for BeforeInstall & AfterInstall which are executed in AppSpec.yml file.
- BeforeInstall: BeforeInstall script contains the tasks need to be executed before installing the Application.
- Example of BeforeInstall script
#!/bin/bash
now=$(date +"%m_%d_%Y")
sudo killall nodejs
sudo mv /home/ubuntu/production /home/ubuntu/production_old_$now
sudo mkdir /home/ubuntu/production
In the above example we are using nodejs & before deploying the latest build we will
stop all the processes of nodejs & will change the name of the current
code directory with today’s date.
- AfterInstall: AfterInstall script contains the tasks need to be executed after installing the Application.
- Example of BeforeInstall script
#!/bin/bash
cd /home/ubuntu/production/weone-backend
sudo chown -R ubuntu:ubuntu /home/ubuntu/production
sudo NODE_ENV=production nohup nodejs app.js > /dev/null 2> /dev/null < /dev/null &
In the above script, we are changing the ownership of our application folder
& starting application process.
Note: Use “/dev/null 2> /dev/null < /dev/null &” to get out of nohup shell automatically,
else your CodeDeploy would stuck at AfterInstall event.
Performing Deployment:
- Launch EC2 instance with IAM role you created earlier & put the
following commands in userdata:- sudo apt-get update
- sudo apt-get install python-pip
- sudo apt-get install ruby2.0
- cd /home/ubuntu
- wget https://bucket-name.s3.amazonaws.com/latest/install
- chmod +x ./install
- sudo ./install auto
- Go to CodeDeploy service in AWS Management console.
- Click on Custom Deployment.
- Click on Create Application.
- Now you need to enter
- Application Name:<Any name>
- Deployment Group Name: Name of deployment group on which you want to deploy app.
- EC2 tags: Add instances in this list on which you want to deploy code using CodeDeploy. Search & add by name.
- Deployment configuration: It contacts 3 options
- CodeDeployDefault.AllAtOnce : It deploys code in one go.
- CodeDeployDefault.HalfAtOnce : Deploys code in half instances at once.
- CodeDeployDefault.OneAtOnce : Deploys code one by one on instances.
- Service Role: We created service role in pre-requisites.
- Click “Create Application”
Creating deployment
- After creating application, we have to create new deployment.
- We need to enter the following:
- Application name: Name of application we created in steps above.
- Deployment groups: Group we created in steps above.
- Revision type: From where we want to download bundle/build
of application, S3 or Github. - Revision Location: Path of bundle/build on S3/Github.
- File Type: Select file type.
- Deployment Description: Enter description about Deployment(optional).
- Deployment Config: Select according to your need but It’s safe to
select “CodeDeployDefault.OneAtOnce”.
- Click on Deploy now.
- After that deployment process will start & we will be redirected to another page.
- We can monitor deployment by clicking view events.
- After completion of deployment, status will be changed to “Succeeded”
from “In Progress”.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankit Arora
Ankit is a Redhat Certified Engineer and Cloud Engineer.