How to spin up an EC2 instance on AWS and install Jenkins.
Tags: beginners, tutorial, jenkins, aws
Published: 2023-08-31 04:45:06

1. Spin up EC2 instance on AWS
2. SSH into EC2 instance 
3. Install Jenkins on Ubuntu LTS

#### Step 1 : Spin up EC2 instance on AWS
Click [link](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html) and follow instructions to spin up EC2 instance on AWS

#### Step 2 : SSH into EC2 instance 
- 
Click [link](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-to-linux-instance.html) and follow instructions to SSH into EC2 instance or 
- 
If you are on windows and want to connect to EC2 instance follow instructions. 

> Prerequisite: *Downloading .pem file is mandatory while creating an EC2 instance. Otherwise, you will not be able to SSH to EC2 instance from your Mac/Windows/Linux.* 

1. Download [mobaxterm](https://mobaxterm.mobatek.net/download-home-edition.html) on windows. 
2. Unzip file and install on windows.
3. Click on Sessions -> New Sessions --> SSH --> 
4. Enter public IP address of EC2 instance in "remote host" input text box
5. Click checkbox "specify username" as "ubuntu"
6. click on "Advanced SSH settings" 
7. Click checkbox "use private key" and locate .pem file in  "downloads" folder.  

![ Settings in mobaxterm to SSH to EC2 instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78dyilb9s67fzc7ul0bi.jpg) 
 
#### Step 3 : Install Jenkins on Ubuntu LTS

1. Click on "Linux"--> [Debian/Ubuntu] (https://www.jenkins.io/doc/book/installing/linux/#debianubuntu) section.
2. Update Ubuntu `sudo apt update`
3. [Install](https://ubuntu.com/tutorials/install-jre#2-installing-openjdk-jre) java in Ubuntu `sudo apt install openjdk-17-jre` or To install Openjdk 11 in Ubuntu (VM VirtualBox), the following commands worked well. ```sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt install openjdk-11-jdk```
4. verify java `java --version` 
5. Install [Jenkins] (https://www.jenkins.io/doc/book/installing/linux/#debianubuntu) 
```
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
```
 
- verify Jenkins installed `sudo systemctl status jenkins` or run `ps -ef | grep jenkins`
``` 
ubuntu@ip-10-0-0-27:~$ ps -ef | grep jenkins
jenkins     4973       1  7 19:55 ?        00:00:59 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080
ubuntu      5240    1458  0 20:08 pts/0    00:00:00 grep --color=auto jenkins
```
```
jenkins.service - Jenkins Continuous Integration Server
     Loaded: loaded (/lib/systemd/system/jenkins.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-08-31 02:55:51 UTC; 1h 29min ago
   Main PID: 8548 (java)
      Tasks: 40 (limit: 1141)
     Memory: 492.5M
        CPU: 1min 11.340s
     CGroup: /system.slice/jenkins.service
             └─8548 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080

Aug 31 03:10:03 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:03.478+0000 [id=243]        INFO        jenkins.InitReactorRunner$1#onAttained: Listed all plugins
Aug 31 03:10:03 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:03.479+0000 [id=243]        INFO        jenkins.InitReactorRunner$1#onAttained: Prepared all plugins
Aug 31 03:10:03 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:03.550+0000 [id=244]        INFO        jenkins.InitReactorRunner$1#onAttained: Started all plugins
Aug 31 03:10:03 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:03.554+0000 [id=244]        INFO        jenkins.InitReactorRunner$1#onAttained: Augmented all extensions
Aug 31 03:10:03 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:03.906+0000 [id=244]        INFO        jenkins.InitReactorRunner$1#onAttained: System config loaded
Aug 31 03:10:03 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:03.910+0000 [id=244]        INFO        jenkins.InitReactorRunner$1#onAttained: System config adapted
Aug 31 03:10:03 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:03.914+0000 [id=244]        INFO        jenkins.InitReactorRunner$1#onAttained: Loaded all jobs
Aug 31 03:10:03 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:03.937+0000 [id=244]        INFO        jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated
Aug 31 03:10:04 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:04.282+0000 [id=244]        INFO        jenkins.InitReactorRunner$1#onAttained: Completed initialization
Aug 31 03:10:04 ip-10-0-26-238 jenkins[8548]: 2023-08-31 03:10:04.289+0000 [id=73]        INFO        h.m.UpdateCenter$CompleteBatchJob#run: Completed installation of 81 plugins in 26 sec
```

- Go to browser and enter public IP Address of EC2 instance.`http:3.54.8.89:8080` 

> If you do not see anything on browser then AWS default security group might turned off port '8080' which jenkins run on. You need to [enable ](https://saturncloud.io/blog/ec2-how-to-add-port-8080-in-security-group-a-comprehensive-guide/) that. 

- open browser and type ip address of instance along with port number .`http:3.54.8.89:8080` 

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

- Go to "Mobaxterm" and view password --> type 
`cat /var/lib/jenkins/secrets/initialAdminPassword`

- go to browser, copy & paste password onto admin field and click "continue" 
- There you go, you have successfully installed the jenkins CI/CD tool onto Ubuntu EC2 instance all by yourself. Congrats. 

*References:* 
Watch Video by [Abhishek.Veeramalla] (https://www.youtube.com/watch?v=Dc0t4LDOySY&list=PLdpzxOOAlwvLNOxX0RfndiYSt1Le9azze&index=4)