Tech Blog

How to setup Jenkins on AWS

How To Setup A Jenkins Server on AWS

Recently for work I had to setup a Jenkins server for using continuous integration with GitHub, I found that some guides were lacking in trouble shooting so I hope that this serves as an entirely complete guide. So lets get started.


Creating the EC2

AWS Ec2.png
  • Select Ubuntu 16.04 LTS (you can use Ubuntu 18.04 but this guide is tailored towards 16.04)
  • Log into your AWS Console
  • Search for EC2
  • Select Create Instance
 
.

.

  • Select your instance size, the micro is included in the free teir
  • Select Create an IAM Role (unless you already have one)

EC2 IAM Role

  • Create an IAM Role then select it si for an AWS Servive, then for EC2
 
  • Select your permissions, I selected SSM, Lambda, and S3 for my Jenkins server permission
  • After you create this IAM role you should save the Access Token and Shared Secret somewhere as you cannot see the shared secret again.
  • I finalize your role here, then go back to your EC2 instance tab, hit the refresh button next to the iam role slection not on the webpage, then select the user you just created
  • Hit launch and wait for the EC2 to be up and running, should only take a minute or two

Mission Control we have launched!


Connecting To Your EC2

This is where I have to not that I was using a Mac at the time to use my terminal to SSH, so you might have to look elsewhere on how to connect initally but afterwards all of the commands to setup Jenkins will be the same

  • Go to your EC2 instance page and hit connect to instance (you should see a popup window), when launching the EC2 you should have downloaded a .PEM file, move that to a safe folder, for me it was Documnets/SSH, because I couldn't load it into the root ssh folder

Open up terminal and enter this command

chmod 400 user/USERNAME/Documents/ssh/NAMEOFPEMFILE.PEM

If you try to connect to your ec2 without this file being super locked it will not let you.

Now to connect to your ec2 enter this, you can copy it from that popup window, of you have issues connecting due to the key being public please refer to the above command

ssh -i Path To PEM File ubuntu@ec2_public_dns

Then it should look like this:

Screen Shot 2018-12-10 at 5.52.58 PM.png

We're Connected!


Installing Jenkins

This is where things got a little tricky for me, so I hope that I can guide you how to get everything setup properly.

Commands to get some packages:

sudo apt update
sudo apt install yum
sudo apt install opened-9-jre
sudo apt install opened-8-jre

Commands to get Jenkins installed:

wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

Right here is where I ran into some errors, if you are getting an error with Jenkins it is because it's location of the JRE is wrong. Here is how we remedy that.

  • We open the Jenkins Config file with vim.
    sudo vi /etc/init.d/jenkins
  • Append path to the new java executable (line 16)
    PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/lib/jvm/java-8-openjdk-amd64/bin/
  • To quit and save vim
    :x

Next we have to make sure we are using Java 8 as our default Java enviornment (needed JRE 9 for other things, I think....)

Enter this command:

sudo update-alternatives --config java
My JRE 8 was 1 so I set it to 1

My JRE 8 was 1 so I set it to 1

Set it to whatever your JRE 8 seleciton number is. A few more commands and we should be all set:

sudo ufw allow 8080
sudo ufw status

If it wasn't enabled:

sudo ufw enable
sudo ufw status
Screen Shot 2018-12-11 at 10.53.59 AM.png

Lastly:

sudo systemctl start jenkins
sudo systemctl status jenkins

And Jenkins should be up and running!

Jenkins I set you free into the world!


Configuring Our Dear Jenkins

Quickly copy this password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

On your EC2 page your Jenkins Server Instance should have a public ip so in your browser go to:

http://Ip Address:8080
unlock_jenkins.png
  • Log in using that password you copied earlier
    sudo cat /var/lib/jenkins/secrets/initialAdminPassword
  • You can either choose to install your plugins manually like I did or use the prebuild ones (probably manually)
  • Oh beautiful plugins how you will help me so with all of that continuous integration that I such desire
  • Create that first admin user and you're good to go!
create-first-user.png

Congradulations you now have your own Jenkins Server!

dan flan