DEV Community

Cover image for Setup AWS CI/CD with Jenkins.
Kanavsingh
Kanavsingh

Posted on

Setup AWS CI/CD with Jenkins.

Problem Statement –
Setup a CI/CD pipeline using the tools your choice(or preferably the mentioned tools).

It should deploy a simple web application to a server on a code push to a
repository.
The deployed web application should be reachable on any web browser.
Make it scalable such that when load increases the number of servers scale
up and down making sure the new servers have the updated code.
Additional requirements
Setup to be done using AWS, Jenkins, CodeDeploy
Jenkins should not be on the same server as the application being
deployed to
Tools 1. Jenkins 2. Git/Bitbucket 3. AWS EC2 4. AWS CodeDeploy
Solution –

Image description
Step 1 – Create Two IAM roles as –

In first provide AmazonEC2RoleforAWSCodeDeploy and AmazonS3FullAccess
In second provide codedeplyrole role.
This roles will be used further.

Image description

Image description
Step 2 – Create 4 ec2 instances and put user data in it –

Image description

Image description

Image description

Image description

User data i have used -

!/bin/bash

sudo yum -y update
sudo yum -y install ruby
sudo yum -y install wget
cd /home/ec2-user
Wget
https://aws-codedeploy-ap-south-1.s3.ap-south-1.amazonaws.com/l
atest/install
sudo chmod +x ./install
sudo ./install auto
sudo yum install -y python-pip
sudo pip install awscli

  • This user data includes installation of some software like aws codedeploy , aws cli etc.
  • add first IAM role to the instances .
  • Create Security Group and provide http 80 and ssh 22 rules.
  • Than create instances.

Step 3 - Create an of any of the instance which we
will use in AutoScalingGroup.

Image description

Step 4 - Create Application Load Balancer.

  1. Create target group than load balancer , in target group give path to /index.html , which is the app file containing code over git.

Image description

Now Create Application Load Balancer -

Image description

Image description

Image description

Image description

We have added the targets to already created instances.

Image description
Step 5 - Create Launch Configurations and than
create Auto scaling group.

Image description

Image description

Image description
Launch Configuration created.
Now create Auto Scaling Group

Image description

Image description

Image description
I have attached Application Load Balancer to it which is already
Created.

Image description
Give Desired and max capacity to 2

Image description
Auto Scaling Group is created.
We can see as soon as ASG created , the two instances are up.

Image description
Step 6 - Create one Jenkins Instance and install
jenkins software in it.
I have used the below link to install JENKINS server into the ec2
instance -
https://www.jenkins.io/doc/tutorials/tutorial-for-installing-jenkins-onAWS/
NOTE - i have also installed git in it , using Command -

  • yum install git -y

Step 7 - Create CodeDeploy

Image description

Image description
Now Create Deployment Group.

Image description
Now Choose Amazon Ec2 ASG which we have Created earlier.

Image description
Now choose Application Load balancer which we have created
earlier.

Image description

Deployment Group Is Created.
Now we will use Jenkins for Creating a pipeline that will use git as a
source and will run the CodeDeploy.

Image description

Step 8 - Create an S3 bucket.

Image description

Step 9 - Go to Jenkins Server

Image description

  • Install AWS Code Deploy plugin in it

Image description

Image description

Image description
Create a jenkins pipeline by taking GIT as a source , Trigger - Poll
SCM ( * * * * * ) which means it will run job in every minute.
Put S3 Bucket name etc.

Image description

Image description
Choose post-build-action as deploy an application AWS
CodeDeploy

Image description
Than give all info of CodeDeploy like app name , deployment group
name , s3 bucket and prefix name.

Image description
Than for Authorization we can use Aws Acess keys and Secret
keys.

Image description
Step 10 - Build Jenkins Pipeline

Image description
Check console output.

Image description
As soon as we will hit the Build Now button the job will run and
trigger for code deploy deployment . we can see here it is in
progress.

Image description

Image description

Image description
Here we can see , it take time , as we have choosed the option of
build all at once.
Step 11 -
Go to AWS Load balancer portal and go to that
particular ELB you have Created and copy the DNS and
paste it on browser , your application will be running .

Image description

This is the first version of application. Now if i made any change to
the code it will automatically reflect to the Web browser within some
minutes.

Image description
We can check that as soon as we run the jenkins job it run the
deployment the data will get stored in s3 bucket.

Image description

Image description
We can see as soon as we changed the code in index.html file , a
new deployment will be triggered by jenkins job .

Image description
IN PROGRESS...

Image description

Image description

Image description
And here is the Our Final Output .
We can see that it shows the recent changed done by us in the html
file.
Since we have used Auto Scaling Group and using ELB and its url
to access the application , we can confirm that it scalable such that
when load increases the number of servers scale up and down
making sure the new servers have the updated code.

Pardon me if I missed any step in between.

Thank You

Top comments (0)