Github and Code Deploy

Lets start with easy one, we will use Github and will deploy application to EC2 through Code Deploy

AWS CodeDeploy is a new service that helps address these challenges by simplifying code deployments to Amazon EC2 instances. AWS CodeDeploy is targeted at customers who manage their EC2 instances directly, instead of those who use an application management service like AWS Elastic Beanstalk or AWS OpsWorks that have their own built-in deployment features. AWS CodeDeploy allows developers and administrators to centrally control and track their application deployments across their different development, testing, and production environments. The service scales with your infrastructure so you can easily deploy to one EC2 instance or dozens.

In addition to deploying applications from Amazon Simple Storage Service (Amazon S3) buckets, AWS CodeDeploy can also deploy direct from GitHub, the popular code management and developer collaboration tool.

Let’s start first by creating 2 IAM roles we will use in this tutorial:

  • IAM role for CodeDeploy to talk to EC2 instances.

  • IAM role for EC2 to access S3.

1 — CodeDeployRole

Go to AWS IAM Console then navigate to “Roles“, choose “Create New Role“, Select “CodeDeploy” and attach “AWSCodeDeployRole” policy:

2 — EC2S3Role

Create another IAM role, but this time choose EC2 as the trusted entity. Then, attach “AmazonS3ReadOnlyAccess” policy:

Now that we’ve created an IAM roles, let’s launch an EC2 instance which will be used by CodeDeploy to deploy our application.

3 — EC2 Instance

Launch a new EC2 instance with the IAM role we created in last section:

Next to User Data type the following script to install the AWS CodeDeploy Agent at boot time:

#!/bin/sh
yum update
yum install -y ruby
cd /home/ec2-user
aws s3 cp s3://aws-codedeploy-us-west-1/latest/install .
chmod +x ./install
./install auto

Note: make sure to allow HTTP traffic in the security group.

Once created, connect to the instance using the Public IP via SSH, and verify whether the CodeDeploy agent is running:

4 — Application

Add the appspec.yml file to the application to describe to AWS CodeDeploy how to manage the lifecycle of your application:

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ec2-user/api
permissions:
  - object: /
    pattern: "**"
    owner: ec2-user
    group: ec2-user
hooks:
  AfterInstall:
    - location: deploy/after_install.sh
      timeout: 1200
      runas: ec2-user
  ApplicationStart:
    - location: deploy/restart.sh
      timeout: 60
      runas: ec2-user
ValidateService:
    - location: deploy/validate.sh
      timeout: 60
      runas: ec2-user

The AfterInstall, will install node server:

source /home/ec2-user/.bash_profile
cd /home/ec2-user/api
npm install

The AfterInstall will restart apache server

source /home/ec2-user/.bash_profile
cd /home/ec2-user/api
npm run deploy

5 — Setup CodeDeploy Create application and deployment group like aws-code-deploy-github as application and aws-code-deploy-github-grp as group While creating group specify target as EC2 instance and add tags defined for EC2 instance

Now Deployment group has been created

Now we can create deployment in this group using Github or using S3, code deploy can take either code commit, Github or S3 as source

Create deployment by adding git commit ID as this deployment is using github

Last updated