Setting up Jenkins on AWS
Tags: devops, aws, opensource
Published: 2020-06-04 18:16:27

This tutorial walks you through the process of deploying a Jenkins application.
* You will launch an EC2 instance (centOS), install and configure Jenkins.
* Jenkins to automatically spin up Jenkins build slave instances if build abilities
need to be augmented on the instance. 

### Launch and Setup an EC2 instance

First, launch an ec2 instance to install and run jenkins. Steps involved are: 
1. launch an ec2 instance
   
![Screenshot](https://user-images.githubusercontent.com/23367724/80366539-0ef3dc00-88a7-11ea-9ce6-7cab66d8f8ea.png)

2. Add secutiry group

![image](https://user-images.githubusercontent.com/23367724/80366758-6b56fb80-88a7-11ea-996b-40755fcf14f4.png)

3. ssh into the instance: select initiated instance, proceed to connect and follow the instructions. Make sure you are in the same folder as your downloaded .pem key.
   ```
    chmod 400 jenkins-ec2.pem //if you named the instance same
    ssh -i "jenkins-ec2.pem" ec2-user@ec2-13-127-31-75.ap-south-1.compute.amazonaws.com //please check your details
   ```

### Install and run Jenkins

Here, we will connect to the instance and run the jenkins after all necessary installation.
Steps covered:
1. connect to your instance
2. download and install jenkins
3. configure jenkins
   
After connecting to your ec2 instance via ssh, jenkins need to be download and installed. 

```
    sudo yum update -y //update system
    sudo yum install java-1.8.0-openjdk-devel //install jdk
    curl --silent --location http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo | sudo tee /etc/yum.repos.d/jenkins.repo //get GPG key
    sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key //add repo to system 
    sudo yum install jenkins //install jenkins

    // start the service
    sudo systemctl start jenkins
    //check service status
    systemctl status jenkins.service

```
On checking jenkins service status, an output like this is expected.

![jenkins running screenshot](https://user-images.githubusercontent.com/23367724/80367483-d0f7b780-88a8-11ea-84be-81e49ca1c995.png)

Voila! Jenkins is installed and running at port 8080 on your instance. 
But remember, there is no access to port 8080 to outside world. Security groups are used for the purpose of protecting our running instances from outside attack. Let's go to attached security group and edit inbound rules. 

![Screenshot 2020-04-27 at 5 30 36 PM](https://user-images.githubusercontent.com/23367724/80371669-e3c1ba80-88af-11ea-8c8c-4253f054dbae.png)

* Open a browser window and access your jenkins instance running on your_ec2_public_ip:8080
  
* access your initial administator password for jenkins using (ec2 terminal). 
  
```
    sudo cat /var/lib/jenkins/secrets/initialAdminPassword
```

* Select install selected plugins and wait for it. Its almost done.
Create a user if needed other than admin. Jenkins ready screen will appear. We are all set to automate our CI process.

![create-jenkins-user](https://user-images.githubusercontent.com/23367724/80369152-6b58fa80-88ab-11ea-965d-39a07817094f.png)

Go to manage jenkins and toggle to install plugins. 

![Screenshot 2020-04-27 at 5 23 19 PM](https://user-images.githubusercontent.com/23367724/80369405-d73b6300-88ab-11ea-9fea-2b299922542d.png)

Jenkins is up and running on AWS.
"Please note for security reasons, never expose your public IP. Always have it running behind a reverse proxy."

In next article, we will create a Jenkins project and integrate it with github. That will be the first step to create a Continuous Integration pipeline.

P.S. "Above written are my views. I am always learning and exploring new things. Please comment and criticize wherever possible. Stay Happy."



### Helpful links
1. https://jenkins.io/
2. [github on centOS](https://linuxize.com/post/how-to-install-git-on-centos-7/)