Constructing a Foundation for Automation: Using Terraform to Create a Jenkins Server on an EC2 Instance

I am very excited to get back to DevOps (Software Development and Operations). Today, we have an awesome Week 20 Project scenario with objectives to accomplish!

SCENARIO: Your team would like to start using Jenkins as their CI/CD tool to create pipelines for DevOps projects. They need you to create the Jenkins server using Terraform so that it can be used in other environments and so that changes to the environment are better tracked. For the Foundational project you are allowed to have all your code in a single main.tf file (known as a monolith) with hardcoded data. Push all your code to GitHub and include the link to your repo in your documentation.

OBJECTIVE 1: Deploy one EC2 Instances in your Default VPC.

OBJECTIVE 2: Bootstrap the EC2 instance with a script that will install and start Jenkins. Review the official Jenkins Documentation for more information: Information Link

OBJECTIVE 3: Create and assign a Security Group to the Jenkins Security Group that allows traffic on port 22 from the IP and allows traffic from port 8080.

OBJECTIVE 4: Create a S3 bucket for the Jenkins Artifacts that is not open to the public.

OBJECTIVE 5: Verify that you can reach that Jenkins install via port 8080 in the browser. Be sure to include a screenshot of the Jenkins login screen in the documentation.

NOTE: Push the code to GitHub and include the link in the write up.

DEFINITIONS:

JENKINS is a platform for creating a Continuous Integration/Continuous Delivery (CI/CD) environment. The system offers many different tools, languages, and automation tasks to aid in pipeline creation when developing and deploying programs. Jenkins is an automation server written in Java that helps build, test, and continually deploy software. The open-source system is currently one of the leading automation servers.

CI/CD PIPELINE (Continuous Integration and Continuous Deployment) pipeline is a series of steps that must be performed in order to deliver a new version of software. CI/CD pipelines improve software delivery throughout the software development life cycle via automation. By automating CI/CD throughout development, testing, production, and monitoring phases of the software development lifecycle, organizations are able to develop higher quality code, faster. Information Link

BOOTSTRAPPING in AWS means to execute some commands during boot up(launch). Execution can easily occur by loading script in user data section during EC2 launch. Bootstrap scripts run only once — when the instance is instantiated for the 1st time. Information Link

TERRAFORM is an infrastructure as code (IAC) tool that allows users to automate various infrastructure tasks. It also allows users to define both cloud and on-premises resources in human-readable configuration files that users can version, reuse, and share. Users can then use a consistent workflow to provision and manage all infrastructure throughout its lifecycle. Information Link

IAC allows developers to codify infrastructure in a way that makes provisioning automated, faster, and repeatable. It’s a key component of Agile and DevOps practices such as version control, continuous integration, and continuous deployment. Information Link

AMI, Amazon Machine Image, is a template for Amazon EC2 instances, created from an Amazon EC2 instance.

PREREQUISITES:

  • AWS Account with IAM Role
  • GitHub Account
  • Basic Knowledge of Jenkins Server
  • Basic Knowledge of Terraform
  • Knowledge of EC2 Instances
  • Basic Knowledge of Cloud9
LET’S GET STARTED!!!
LET’S GET STARTED!!!

STEP 1: In GitHub, Create a New Public Repository, a New Branch Protection Rule & a New Token for this Project.

You will need to create a new repository (repo) in which to save your Terraform Project. To create a new public GitHub Repository, select the “+” sign — Select New repository — Under repository name, name the repository and give it a description — select Public — Under Add.gitignore Terraform — Create repository.

You will need to add branch protection for your new repository’s branches in order to protect your branch(es) from unwanted changes by others and your own mistakes.

Click Settings — Click Branches in left column — Click Add branch protection rule — The name of the branch pattern is “main” — see the boxes to check below — Click create.

You need to create a new token to protect your account and work. Make sure to save your token as your may need it, again. These tokens are account specific and not repo specific.

From the main selection panel, select Settings — In left column, select Developer settings — In the left column, under Personal access tokens, click Tokens (classic) — Click Generate token — Click Generate new token (classic) — For new Personal access tokens (classic), under Note, enter a name for the token — Select a number expiration days — Click repo — Click Generate token

REMEMBER TO SAVE YOUR PASSWORD!!! YOU WILL NOT SEE IT AGAIN!!!

STEP 2: Create a new CLOUD9 IDE.

Now, let’s create a new Cloud 9 environment. In AWS, type Cloud9 in the CLI — Click Create environment — On the Create environment page, in the Details Section, under Name enter a name for your new environment — Under Description, enter a description — Under Environment type, click New EC2 instance — In the New EC2 Instance section, under Instance type click t2.micro — Under Platform select the Amazon Linux 2 — Under Timeout, select 30 minutes — In the Network Settings section, under Connections click AWS Systems Manager (SSM) — Under Amazon VPC, select the default VPC — Click Create

STEP 3: Create a New Directory in Cloud9 and several Terraform files for this project.

Below is the code for creating the new directory:

In the new directory, create new folders with the .tf and .sh extensions.

STEP 4: Deploy a New EC2 Instance in the Default VPC. Bootstrap the EC2 Instance with a Script that will Install and Start Jenkins.

In the main.tf file — Install Terraform (If you need to install Terraform)Configure the AWS Provider Create an EC2 instance in Cloud9 using Terraform

NOTE: I retrieve my AMI by pseudo-launching an instance and grabbing the AMI. I did not complete the process of launching the instance, I just clicked into the process to grab the AMI, then I cancelled the process.

main.tf folder — Bootstrap the EC2 instance with a script that will install and start Jenkins

STEP 5: Create and Assign a Security Group to the Jenkins Security Group that allows Traffic on Port 22 from the IP and allows traffic from Port 8080.

main.tf folder — Create and assign a security group to the Jenkins EC2 instance that allows traffic on port 22 from your IP address and allows traffic from port 8080. Input your VPC in this script.

STEP 6: Create an S3 Bucket for the Jenkins Artifacts that is not open to the public.

main.tf Create Terraform configuration to create an S3 bucket for Jenkins Artifacts that is not open to the public

STEP 7: Verify that the Jenkins Install is reachable via Port 8080 in the browser. Include a screenshot of the Jenkins login screen in the documentation.

Write —Stage to create changes to the code. (Not in illustration)

Init —Stage to initialize the code to download the requirements mentioned in your code. Type terraform Init

Validate- Stage to validate the configuration files in a directory, referring only to the configuration and not accessing any remote services. Type terraform validate

Plan — Stage to review changes and choose whether to accept the changes. Type terraform plan

Type terraform fmt this command is used to rewrite Terraform configuration files to a canonical format and style

Type terraform plan -out=myplan.out
The “-out” option in Terraform is used to save a generated execution plan to a file. This allows you to apply the plan at a later time or share it with others. To use this option, simply include the “-out” flag followed by the filename where you want to save the plan. For example, “terraform plan -out=myplan.out” will save the generated plan to a file named “myplan.out”.

Apply — Stage to accept changes and apply the changes against real infrastructure. Type terraform apply

Verify Jenkins install IS REACHABLE via port 8080 in your browser. Enter the command below to reach your Jenkins server.

http://myipaddress:8080

Destroy — Stage to destroy all created infrastructure. Type terraform destroy.

STEP 7: Push to code to GitHub Account and include the link in the write up.

Create a branch. I like to use the date that I initiate a project.

# Clone GitHub Repository and then cd into the repo.

git clone <git hub http>

# Commit the Repo to the branch and then cd into the branch.

cd <Repo name>
# Check Status files to be committed
git status
Add files to the repo and check status again after you have committed. Files should be green.
git add .
git status
# To commit files
git commit -m "Give a description"
You will have to give your username and email for this action.
# To push to file GitHub repo that was cloned
git push origin

or

git push <repo http>

In GitHub, commit the new changes to the main branch.

Merger branches then deleted the branch. Keep the main branch.

My GITHUB link to this project.

Congratulations!

You have successfully created a Jenkins Server on an EC2 Instance using Terraform and pushed the code to your GitHub Account!

--

--