Saturday, 23 October 2021

Deploy Jenkins with Terraform

REFERENCE THE LINK INSTALLING TERRAFORM ON YOUR LOCAL MACHINE BEFORE STARTING THIS LAB.

The goal is to automate the installation of Jenkins on EC2 instance. This lab will walk you through configuring and installing Jenkins with userdata.


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 file main.tf and copy the below code in yellow color


















provider "aws" {

        access_key = "ACCESS KEY"
        secret_key  = "SECRET KEY"
        region         = "us-east-2"

}

resource "aws_instance" "jenkins" {
    user_data   = base64encode(file("deploy.sh"))
    ami                                = data.aws_ami.ubuntu.id
    instance_type                = "t2.micro"
    vpc_security_group_ids = [aws_security_group.jenkins_sg.id]
    root_block_device {
    volume_type                  = "gp2"
    volume_size                   = 200
    delete_on_termination   = true
    encrypted                       = true
  }

  tags = {
    Name = "u2-cicd-jenkins"
  }

}

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.jenkins.*.private_ip]

}


output "ec2_ip_public" {

    value = [aws_instance.jenkins.*.public_ip]

}


output "ec2_name" {

    value = [aws_instance.jenkins.*.tags.Name]

}


Bash Script to Deploy Jenkins

  • Create a new file deploy.sh and copy the below code in yellow color. 

#!/bin/bash

echo This is installing 3

sudo apt-get update

sudo apt-get install default-jdk -y

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'

echo This is installing 9

sudo apt-get update

echo This is installing 10

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 apt-add-repository ppa:ansible/ansible

sudo apt install ansible

sudo apt-get install python -y

sleep 5  

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


Step IV: Create a new file data.tf and copy the below code in yellow color


data "aws_ami" "ubuntu" {

    most_recent = true


    filter {

        name   = "name"

        values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]

    }


    filter {

        name   = "virtualization-type"

        values = ["hvm"]

    }


    owners = ["099720109477"] # Canonical

}



Step V: Create a new file security.tf and copy the below code in yellow color

resource "aws_security_group" "jenkins_sg" {
name = "jenkins-dev-sg"
description = "EC2 SG"

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

   ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

#Allow all outbound
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
    Name = "jenkins-dev-sg"
  }
}


Step VI: Open Terminal in VSCode



















Step VII: Execute command below

terraform init
the above command will download the necessary plugins for AWS.

terraform plan
the above command will show how many resources will be added.
Plan: 2 to add, 0 to change, 0 to destroy.

Execute the below command
terraform apply
Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  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.

No comments:

Post a Comment

How to upgrade Maven

  java.lang.IllegalStateException I had installed maven in my ubuntu using command  apt install maven This installed maven in path /usr/shar...