Continuous Delivery of code from GitHub to EC2 with AWS Code Pipeline – (Part 1)

  • Published
  • Posted in AWS
  • Updated
  • 4 mins read
  • Tagged as

In software development, automating code deployment has become a crucial component of DevOps process, as it enables developers and operation team to work seamlessly.

Here comes AWS Code Pipeline, which is a fully managed continuous delivery service that allows users to build, test and deploy code.  It orchestrates other services like – Code Commit, Code Build and Code Deploy to complete deployment pipeline.

One could use any of them for code repository – Code Commit, GitHub, or Bitbucket. Code Build is used to build your code if needed.

For deployment, Code Deploy is used.

For Infrastructure Configuration, one could use CloudFormation, Elastic Beanstalk, ECS, etc.

In this tutorial, we are going to use GitHub as source code, Code Build to build the artifacts and Code Deploy to deploy our code to EC2 Instance. We will create a pipeline where any changes made in GitHub will trigger the Pipeline and the latest code version will be deployed on EC2 instance.

  1. Pre-requisites

To create CI/CD pipeline, you must have:

  • GitHub account
  • AWS account

2.Create and Launch EC2 instance.

To set up an EC2 instance, navigate to EC2 console of your AWS account.

Add Name to the instance. We have named it “CI/CD Instance”.

Select AMI for instance. We have used Amazon Ubuntu AMI (free tier).

Next step is to create a Key Pair. If you have a Key Pair, you can use that. If not, create a new Key Pair.

Create a new security group, select the options as shown in image below.

All the rest options can be kept as default.

Go ahead and launch the Instance. Wait until the instance is live and running.

Now we need to connect to our EC2 instance either using console or any secure shell client. We will be using MobaXterm here.

Before that we need to create a .ppk file of the Key Pair we created while launching the Instance.
We can use PuttyGen for this.

Click on Load and select the .pem file created in above procedure.

You will get a prompt (PuttyGen Notice), click OK and save private key

This private key will be used to connect EC2 instance using MobaXterm.

Open MobaXterm and start a SSH session.

We will get a window prompt, click on SSH, enter Instance’s Public IP in Remote Host, Specify User name “ubuntu”. Under “Advanced SSH Settings”, tick the check box to “use private key” and load your .ppk file. Then click OK and accept the connection.

You will be connected to EC2 instance.

Install Git utility (latest version) on your instance.

Cmd : sudo apt install git -y

Now we need to install code-deploy agent on instance as Code Deploy service will be used to deploy code from GitHub to Instance.

To install code-deploy agent, run following commands.

sudo apt-get update -y

sudo apt install -y ruby

sudo apt install wget

wget https://aws-codedeploy- us-east-1.s3. us-east-1amazonaws.com/latest/install

chmod +x ./install

sudo ./install auto

sudo systemctl enable codedeploy-agent

sudo systemctl start codedeploy-agent
sudo systemctl status codedeploy-agent

Note: Since my Instance and Code deploy service is in US-east-1, I have mentioned it as us-east-1 in above wget cmd. If you have Instance in other region, please change it to respective region identifier wherever us-east-1 is specified.

3. Create an IAM role for EC2 instance.

In IAM Console, Navigate to Create Role, select EC2 and proceed.

For simplicity, select full access but you can include as needed. Add these permissions –  “S3FullAccess’”, “Amazon EC2 Full Access” and “AWS Code Deploy Full Access”.

I have named the role as “EC2-CodeDeployRole” and Create the Role.

Once created the role, Navigate to EC2 Instance, select the instance, click on Actions, from dropdown click on Security and Modify IAM Role.

Select the role created from drop-down and update IAM Role.

After the IAM Role is attached to Instance, we need to restart the code-deploy service on Instance. SSH into the instance and run cmd

Cmd : sudo systemctl start codedeploy-agent

In next blog we will continue with creating Build Project, Code Deploy Application and Creating Code Pipeline.

Addend Analytics is a Microsoft Gold Partner based in Mumbai, India, and a branch office in the U.S.

Addend has successfully implemented 100+ Microsoft Power BI and Business Central projects for 100+ clients across sectors like Financial Services, Banking, Insurance, Retail, Sales, Manufacturing, Real estate, Logistics, and Healthcare in countries like the US, Europe, Switzerland, and Australia.

Get a free consultation now by emailing us or contacting us.