TERRAFORM LAB 3
Before proceeding with this lab, reference the following link below:
In this lab, we will be deploying a Jenkins standalone server with terraform.
Prerequisites:
AWS access and secret keys are required to provision resources on AWS cloud.
- Open Visual Code Studio then click on File > Preferences > Extensions then search and install Terraform extension
- Login to AWS console, click on Username on top right corner and go to My Security Credentials

- Click on Access Keys and Create New Key
Step I: Open File Explorer, navigate to Desktop and create a folder jenkins_workspace.
Step II: Once folder has been created, open Visual Code Studio and add folder to workspace
Step III: Create a new folder files in workspace and follow the below steps:
- In files folder, create a file environment and copy the below code and save it
- In files folder, download JAVA dependencies on your machine and copy it in the directory.
Step IV: Create a new file main.tf and copy the below code in yellow color
region = var.region
access_key = var.accesskey
secret_key = var.secretkey
}
resource "aws_instance" "ec2" {
provisioner "file" {
source = "./files/environment"
destination = "/home/ubuntu/environment"
}
provisioner "file" {
source = "./files/jdk-8u251-linux-x64.tar.gz"
destination = "/home/ubuntu/jdk-8u251-linux-x64.tar.gz"
}
################################################################################################
#### Input code here to configure your jenkins server (yum install, sudo this, sudo that, pip those)
################################################################################################
provisioner "remote-exec" {
inline = [
"sudo pip install awscli",
"echo This is installing 1",
"sudo apt-get install -y unzip",
"echo This is installing 2",
"sudo apt-get install wget",
"echo This is installing 3",
"sudo yum install java -y",
"sudo tar xvf /home/ubuntu/jdk-8u251-linux-x64.tar.gz",
"java -version",
"echo Completed installing java",
"sudo mv -f /home/ubuntu/environment /etc/",
"source /etc/environment",
"echo Set JAVA HOME",
"wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -",
"echo This is installing 8",
"sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'",
"sudo apt-get update",
"sudo apt-get install jenkins -y",
"sudo sed -i 's|/bin:/usr/bin:/sbin:/usr/sbin|/bin:/usr/bin:/sbin:/usr/sbin:/home/ubuntu/jdk1.8.0_251/bin|g' /etc/init.d/jenkins",
"sudo systemctl daemon-reload",
"sudo systemctl start jenkins",
"echo Installation Complete",
"cd /home/ubuntu/",
"wget https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip",
"unzip terraform_0.12.24_linux_amd64.zip",
"sudo mv terraform /usr/bin/",
"sudo pwd"
]
}
ami = "ami-0782e9ee97725263d"
root_block_device {
volume_type = "gp2"
volume_size = 200
encrypted = true
}
tags = {
Name = var.stackname
CreatedBy = var.launched_by
Application = var.application
OS = var.os
}
instance_type = "t2.micro"
key_name = "terraform"
vpc_security_group_ids = [aws_security_group.ec2_sg.id]
#This connection string is to establish a connection via ssh to configure the instance
user = "ubuntu"
type = "ssh"
host = self.public_ip
private_key = file("KEYNAME.pem")
timeout = "2m"
}
}
Add the block below in main.tf to output the Private IP, Public IP and EC2 Name after creation. (Note: This is not required)
output "ec2_ip" {
value = [aws_instance.ec2.*.private_ip]
}
output "ec2_ip_public" {
value = [aws_instance.ec2.*.public_ip]
}
output "ec2_name" {
value = [aws_instance.ec2.*.tags.Name]
}
Name = "jenkins-dev-sg"
}
variable region {
type = string
default = "us-east-2"
}
############## tags
variable accesskey {
type = string
default = "ENTER ACCESS KEY HERE"
}
variable secretkey {
type = string
default = "ENTER SECRET KEY HERE"
}
variable stackname {
type = string
default = "u2-dev-jenas"
}
variable application {
type = string
default = "Jenkins"
}
variable os {
type = string
default = "Ubuntu"
}
variable launched_by {
type = string
default = "ENTER YOUR NAME HERE"
}
Execute the below command
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.