Setup AWS CI/CD with Jenkins.
Tags: aws, cloud, devops, webdev
Published: 2023-03-25 19:50:03

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzltvkcwaeruzihv8wsi.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/teu8b80lgfvy0r3rhejv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvfoodmy9ntwsj44l4ur.png)
Step 2 – Create 4 ec2 instances and put user data in it –

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u51p8otka4ao4xoopndv.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wcneqy6zfq76qphh165k.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oufpzb6bl6budbeo0y8h.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdd99gtn4gjpo2f3tjxv.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k7559q00miqai7jpuw4s.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijtwb4qslmcik1py553v.png)

Now Create Application Load Balancer -


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4sosidqlbh7k9ekte78r.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ksuo33nva5a1buar27w.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocajk2fibxkmygxws0ay.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbrkmz818bgzthf7dy6y.png)

We have added the targets to already created instances.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udy7t3nzolhsw138pz6p.png)
Step 5 - Create Launch Configurations and than
create Auto scaling group.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ui4c0cv2ijuehashhzii.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3wht2v44vxf6glez52j.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3hchq7mk7cghrb7gadim.png)
Launch Configuration created.
Now create Auto Scaling Group


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xgwr4cdaco25dodbc82.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8pzj4i5ja5nb71z9no2.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebyyb1x6zajgftzc0l0z.png)
I have attached Application Load Balancer to it which is already
Created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inqo1fhiv8hjx3v0v3b5.png)
Give Desired and max capacity to 2


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6hg1glo4b0idh7ch9x5.png)
Auto Scaling Group is created.
We can see as soon as ASG created , the two instances are up.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4u0k0e7zjhf91ee7vbps.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56wsdztx3vull3eupr5l.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87fmqgnp68z4gs0nhvnn.png)
Now Create Deployment Group.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hffipqxbn77pwuvch4fm.png)
Now Choose Amazon Ec2 ASG which we have Created earlier.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6ymbr20l3ea8w2qlo9m.png)
Now choose Application Load balancer which we have created
earlier.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6cqje8nfjrzoy8y9c1d.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cecxqc5dimbry3fbxdg6.png)

Step 8 - Create an S3 bucket.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gy81rpf5ihevdle64y5.png)

Step 9 - Go to Jenkins Server

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tj043dkbrdwibt06xhn.png)
- Install AWS Code Deploy plugin in it

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gniufrtshqtes5jtrg4.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfejo8y1n2eqz4qunqd0.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ro4ymyod78msp39s995c.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uffhggyxp8yftaib5q9w.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ir3h1q43p8lio1bd0shy.png)
Choose post-build-action as deploy an application AWS
CodeDeploy

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zv3031h7dqaflpn51wr1.png)
Than give all info of CodeDeploy like app name , deployment group
name , s3 bucket and prefix name.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3luvd9k8z046obpl2jy8.png)
Than for Authorization we can use Aws Acess keys and Secret
keys.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4vslh3o059fxp9m90cv.png)
Step 10 - Build Jenkins Pipeline

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crjaq9y65j5j76xldjkc.png)
Check console output.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/knvef5snnozfh3d9dw6g.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbksjraz5rmn0kbh2d3j.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ax1usuohy4dedf0d5eyz.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgxlqla70t0maeug0dlk.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kg9p723kho04767jqbei.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ufec679l48eq5beif6j.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d9il1jiqi4wach9cz47f.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtgpkit88hbobsdnpji4.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1ucf2cs4g13mcf6tg0y.png)
IN PROGRESS...

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sr1d11uop3rhhhgw4l75.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qkd3ly3yry8lul396ay.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3j1a2u2w3j046yxc51yd.png)
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