Step by step guide to set up a simple CICD pipeline to deploy your code right from Github repository, all automated.

This guide works with Github, CodeDeploy, CodePipeline and EC2 instance with Ubuntu 16.04 or 18.04

Just follow along and you will understand concepts at each step.

Step 1: Setup Github Repo

Use this link to create a new Github Repo.

You can download a sample application from AWS S3 link, which will work for Centos/RHEL based EC2 node. If you prefer Ubuntu like me, you can get the Ubuntu compatible code from my repo:

mkdir /tmp/nandan-repo

git clone https://github.com/nonai/github-codedeploy-cicd-poc.git /tmp/nandan-repo

git clone <your github repo clone url>
cd <your github repo directory>

#copy the necessary files from my repo
cp -rf /tmp/nandan-repo/LICENSE.txt /tmp/nandan-repo/appspec.yml /tmp/nandan-repo/index.html /tmp/nandan-repo/scripts .

Push these to your GitHub Repo:

git commit -am "Adding the sample apps"

git push origin master

Get the Github token of your repo

Goto Github -> Settings -> Developer settings -> Personal access tokens -> “Generate new token”. Note this token ID, which will be required in sometime.

Step 2: Setup EC2 instance

Launch the instance

Launch a Ubuntu 16.04 or 18.04 EC2 instance and make sure you are able to access it using SSH. If you are not sure of it, you can refer to this link to launch a Ubuntu EC2 instance.

Create and attach instance profile

We need instance profile so that the EC2 instance that we just launched can access necessary resources from the S3 bucket. You can follow this link to create and attach instance profile

Step 3: Setup AWS CodeDeploy

What is AWS CodeDeploy?


Create a Service Role for CodeDeploy

A service role is required for the Codedeploy to access EC2 and other AWS resources. To create a service role, please refer to this link.

Set up the CodeDeploy

From the AWS CodeDeploy page, click Create New Application.

Then we need to create a Deployment Group under the application that we just created. Use the service role we created previously.

While creating the deployment group, select the EC2 under environment configuration, and select CodeDeploydefault.AllAtOnce as shown below:

Once you are done creating Deployment group, we now need to create a deployment. See below:

As soon as you save the deployment, it is going to fetch the commit ID from your repo and try to deploy it to your EC2 instance as shown below:

Verify the deployment by trying the Public IP of your EC2 in a browser. For example:

If you are not seeing the above page, or your deployment is failing, then we need to troubleshoot. The below link will be very useful:

https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-ec2-instances.html

We can also check the CodeDeploy log at “/var/log/aws/codedeploy-agent/codedeploy-agent.log” in the EC2 instance.

The next piece to make it completely automated is AWS CodePipeline. Lets take Step 4 in the next part.

Contd…

3 thoughts on “CICD – Get your code from Github to EC2 using Codedeploy – Very simple”

Leave a Reply

Your email address will not be published. Required fields are marked *