Jenkins - Setup a Build Environment in AWS
Tags: jenkins, aws, tutorial
Published: 2020-12-18 19:08:55

## Objective

This guide is simply an updated regurgitation of the AWS guide: [Set Up a Jenkins Build Server](https://aws.amazon.com/getting-started/hands-on/setup-jenkins-build-server/). I mainly wanted to have this for my own reference, but I hope it can help someone else!

My intent is to just be quick and to the point with minimal explanations.

## Prerequisites

This guide assumes you have:

- Basic knowledge of AWS, an AWS account and familiarity with the AWS Console and the EC2 service.

*All resources used are Free Tier eligible.*

*I use macOS, but this guide does cater to Linux and Windows as well.*

## Overview

Setting up a Jenkins Environment in AWS

- [Creating a Security Group](#creating-a-security-group)
- [Creating an EC2 Instance](#creating-an-ec2-instance)
- [Installing Packages on your EC2 Instance](#installing-packages-on-your-ec2-instance)
- [Configuring Jenkins for the First Time](#configuring-jenkins-for-the-first-time)

## Setting up a Jenkins Environment in AWS

Before beginning, log into the AWS console and verify that you are using the region that is closest to you.

### Creating a Security Group

- From the AWS console search for and select the **EC2 service**
- From the left menu, select **Security Groups** and then click **Create Security Group**
- Give the Security Group a **name** (this can be anything you want but usually reflects the underlying permissions: `WebServerSecurity`)
- Add a **description**
- Choose **your VPC** from the list (you can accept the default if you don't have a specific VPC you're targeting)
- On the Inbound tab, click **Add Rule** and add the following rules:

| Type | Protocol | Port Range | Source | Description |
| ---- | -------- | ---------- | ------ | ----------- |
| SSH  | TCP | 22 | Custom (YOUR IPv4)/32 | -- |
| HTTP | TCP | 80 | 0.0.0.0/0, ::/0 | -- |
| Custom TCP Rule | TCP | 8080 | 0.0.0.0/0, ::/0 | -- |

- Click **Create**
- Verify that your security group appears in the list and continue on to the next section.

### Creating an EC2 Instance

- From the left menu in the EC2 service, select **Instances** and then click **Launch Instance**
- From the **Choose an Amazon Machine Image** page, select **Free tier only** and then select the **Amazon Linux AMI 2 (HVM)**
- From the **Choose an Instance Type** page, select the `t2.micro` instance and click **Next: Configure Instance Details**
- From the **Configure Instance Details** page, complete the items below (the rest can be left as the default):
  - **Network** - select your VPC (same as the previous section).
  - **Subnet** - No preference .
  - **Auto-assign Public IP** - Enable.
  - Click **Review and Launch**.
- From the **Review Instance Launch** page click **Edit security groups**.
- From the **Configure Security Group** page:
  - Tick **Select an existing security group**.
  - Choose the security group that you created in the previous section (`WebServerSecurity`).
  - Click **Review and Launch**.
- On the **Review Instance Launch** page, click **Launch**.
- When prompted to **Select an existing key pair or create a new key pair**:
  - Select **Create a new key pair** and then give your key a name (this can be anything you want but usually reflects the use case: `JenkinsSSH`).
  - Click **Download Key Pair** and save it to a location that you can easily access.
  - With the key pair saved, check the **I acknowledge** and then click **Launch Instances**.
- From the **Launch Status** page, click **View Instances** to review the status of the instance you've just launched. 
- Once the **Instance State** is **green** and **running** and the **Status Checks** shows **2/2 checks**, it is ready for use. (This may take a few minutes).
- Once these statuses update, continue onto the next section.

### Installing Packages on your EC2 Instance

- From the **Instances** view in the **EC2 service**, select your instance and copy the **Public IPv4 DNS** which should be located under the **Details** tab (In the older UI this field is called Public DNS and is located under the Description tab).
- Open a terminal and SSH into your EC2 instance: `ssh -i /path/to/<your_key_pair.pem> ec2-user@<your_public_dns>` (replacing the `<angles>` with your info)

> This guide assumes you are using Linux or macOS, but if you are using Windows, you can reference the AWS guide for 
<a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html" target="_blank">Connecting to your Linux instance from Windows using PuTTY</a>

- Type `yes` if prompted: "Are you sure you want to continue connecting?"
- Once you're logged in, enter: 
  - `sudo yum update -y` to update your instance with the latest software packages.
  - `sudo yum install java-1.8.0 -y`
  - `sudo yum install git -y`
  - `sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo` to add the Jenkins repo
  - `sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key` to import a key file from Jenkins-CI enabling the installation.
  - `sudo yum install jenkins -y` to finally install the package
  - Once the installation completes, start the Jenkins service with `sudo service jenkins start`.
- Verify the service started with an `OK` and continue on to the next section.

### Configuring Jenkins for the First Time

- With the Jenkins service running on your EC2 instance, **open a browser and navigate to** `http://<your_public_dns>:8080` **replacing the `<angles>` with your info**.
- From the **Unlock Jenkins** page, enter the following in your SSH session to find the administrator password: `sudo cat /var/lib/jenkins/secrets/initialAdminPassword`. Copy and enter the password into the prompt and click **Continue**.
- From the **Customize Jenkins** page, click **Install suggested plugins**
- Once the installation completes, complete the form to create a secondary user that you will use in the future to access this Jenkins instance. Click **Save Credentials** and then click **Start Using Jenkins**
- Verify that you can now see the Jenkins Dashboard and continue on to the next section. 

## Next Steps

Your Jenkins instance is now ready for you to explore and use. If you're new to Jenkins, try [Creating your first Pipeline](https://www.jenkins.io/doc/pipeline/tour/hello-world/) from the official Jenkins documentation.

If you're familiar with Jenkins, try [Jenkins - Build a Private GitHub Repo]() where I show you how to configure a build to pull from a private GitHub repository.

## Resources

- https://d1.awsstatic.com/Projects/P5505030/aws-project_Jenkins-build-server.pdf
- <span>Photo by <a href="https://unsplash.com/@ryanquintal?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Ryan Quintal</a> on <a href="https://unsplash.com/@ryanquintal?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>