A complete Guide of CI/CD pipeline and deploy your app in EC2

What is codepipeline ?

Pipeline (CI/CD) can be defined as a series of steps that must be performed in order to deliver a software. CI/CD focuses on delivery of softwares smoothly via automation.During the SDLC process any software should pass from different phases like development, testing, production and monitoring, such long processes are automated using CI/CD tools to develop higher quality code, faster and more securely.Implementing the automation process helps to minimize human error and maintain consistent flow of software release.

AWS introduces a service like code pipeline integrated with other services like CodeCommit, CodeBuild and CodeDeploy.

For this practical session we are use following services:

– IAM – identity and access management

– CodeCommit

– CodeBuild

– CodeDeploy

– CodePipeline

– S3 – Simple Storage Service

– EC2 – Elastic Compute Cloud

Now, let dive into Practical example

Step 1  

Click service, choose developer tool and select codecommit

Step 2

Create a repository , give the name of the repository and fill some description about application, code guru remains uncheck for this project

Step 3

So, we need an IAM user for SSH connection and for HTTPS connection, for now I have already created one.

Step 4

Now, generating HTTPS credentials 

Step 5

Next step is cloning the git url into local machine

Using VSCODE terminal for cloning the repository 

Step 6

Buildspec file is created to build our application

Step 7

Now you can start the build and check inside the phase detail whether it is succeed or failed

Step 8

Now, configure the artifact for that select amazon S3

Give the bucket name and folder name

Artifact packaging check for ZIP for further deployment

Step 9

Now, you can again build the project and check for the artifact in the S3 bucket

Step 10

The next step is to create a  deployment application to deploy the app.

To configure the application first provide the application name and compute platform

And create application

Step 11

So far we have continuous integration of the app by using codecommit and codebuild. So for Continuous deployment,

Next step is to create a deployment group.

Provide the deployment group name  and service role for the group

In the environment  configuration section, the deployment environment is an EC2 instance.

For this you can launch EC2 instance and provide info of EC2 as Name and value

For deployment setting choose codedeploydefaultallatonce, if you need load balancer you can enable load balancing or uncheck if needed.

Finally create a deployment group

Step 11

To deploy any application in EC2 we need an agent for codedeploy, on the behalf of codedeploy it helps to install our application to respective platform

To Set up agent ( code deploy agent) you can install following code into machine

 Create a install.sh file and copy the following code and execute it

#!/bin/bash

# This installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.

sudo apt-get update

sudo apt-get install ruby-full ruby-webrick wget -y

cd /tmp

wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/releases/codedeploy-agent_1.3.2-1902_all.deb

mkdir codedeploy-agent_1.3.2-1902_ubuntu22

dpkg-deb -R codedeploy-agent_1.3.2-1902_all.deb codedeploy-agent_1.3.2-1902_ubuntu22

sed ‘s/Depends:.*/Depends:ruby3.0/’ -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control

dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/

sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb

systemctl list-units –type=service | grep codedeploy

sudo service codedeploy-agent status

Step 12

The zip of your project can be found in the S3 , it is created while we CodeBuild

Create zip file of app and copy the s3 URI to deployment

Copy it to the create deployment revision location

Step 13

For the EC2 to deploy any application it need certain permission to deploy now,

Creating ec2-code-deploy role and provide certain permission to deploy application

Step 14

Add your role to instance and restart the EC2  after updating the iamrole

Finally the deployment process is success

Step 15

Now let’s set up codepipeline for continuous integration and continuous deployment

Create a new pipeline provide name of the pipeline, pipeline type, if you have existing role you can select or create new one

For the source stage provide source provider, name of repository and branch name.

You can choose AWS CodePipeline for any changes to detect and start pipeline

In the build section choose AWS CodeBuild, region and the build project you have created

For the deploy stage choose CodeDeploy, your deploy application name and the deployment group

Now you can start the codepipeline by adding some change in your applcation

Step 16

Finally we can see our result in browser, copy your public ip and find the changes

Leave a Comment

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