Date Posted:28-01-2017

In the modern era, deploying the code on the multiple server is a challenging one. In classic, people normally use FTP/SFTP to upload the files and restart application. In this article, we are gonna explain how to use code deploy aws service to deploy the code on aws instances.

There are two ways to deploy the code to the instances.

  1. Aws S3
  2. Git Hub

We are going to cover how to deploy code from git hub. Incase if you want use, amazon s3, please refer amazon code deploy document.

Implementation:

Let’s start with IAM role creation. We need to create 2 IAM roles for the services ec2 and code deploy to communicate each other.

  1. CodeDeployInstanceRole
  2. CodeDeployServiceRole

Create a new Role(IAM > Create New Role ). Enter the Role name as CodeDeployServiceRole and attach the below policy.

+ AwsCodeDeployRole

Create another role. Enter the Role Name as CodeDeployInstanceRole and attach the below policy.

+ AmazonEc2RolePolicyForAWSCodeDeploy
+ AutoScalingNotificationAccessRole

Launching Instance with IAM Role:

From previous step, we have created 2 policies and its time to use these policies at the time of instance launch.

While launching the instance, make sure to select “IAM role” as “CodeDeployInstanceRole” and complete the installation launch process. Please follow the below post to install LAMP, LEMP on the server which we have launched depends on your requirement.

Install apache, php5.6 ubuntu

Install Nginx, Php, MariaDB on Ubuntu 16.04.1

Install Dependency:

In this example, we are using ubuntu host. Lets start with dependency installation.

apt-get -y install python-pip

apt-get -y install ruby

apt-get -y install wget

Download the codedeploy-agent install.

wget https://aws-codedeploy-eu-central-1.s3.amazonaws.com/latest/install

Install the application

chmod +x ./install
./install auto

Start the codedeploy-agent.

service codedeploy-agent start

Deployment Script:

Code deploy requires appspec.yml  files which is mandatory for codedeploy to work. Create appspec.yml and upload it in git repo. The sample configuration can be downloaded from the below URL.

appspec.yml

 

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
    except:
    owner: ubuntu
    group: ubuntu
permissions:
  - object: /var/www/html
    owner: ubuntu
    group: ubuntu
    mode: 755
    type:
      - directory
  - object: /var/www/html
    owner: ubuntu
    group: ubuntu
    type:
      - file

hooks:
  BeforeInstall:
    - location: scripts/install_codedeployment
      timeout: 40
      runas: root
  ApplicationStart:
    - location: scripts/restart_nginx.sh
      timeout: 30
runas: root

Create a directory scripts on git and upload the following files on the git repository.

restart_nginx.sh

 

#!/bin/bash
service nginx restart
Create CodeDeploy Application:

Login to Aws Management Console

Move the pointer to Services > CodeDeploy

Select Create Application

Add the application details


Application name:<Your Application Name>

Deployment group name:<Deployment Group Name>

Deployment type: In-Place Deployment

Add Instances:

  •     Search by Tags

There will be 3 options for deploying the code deploy

  1. Amazon Ec2
  2. Autoscaling Group
  3. On Premises Instance

In our Example, we will use Amazon EC2 with Name tag to the instance which we created. Select the instance.

Deployment configuration: CodeDeployDefault.OneAtaTime

Service role ARN:CodeDeployService


Deploying codedeploy to the instance:

1.Login to AWS Management Console

2.Move to AWS CodeDeploy > Applications

3. Actions > Deploy New Version

4. Select Revision type:My application is stored in GitHub

5. Repository Name:<Your Git Repository>

6. Commit ID:<GIT Commit ID>

7. Click on Deploy

Look into Deployment ID and check the deployment process.

 

Leave a Reply