How to create a Jenkins server using terraform, and AWS EC2

How to create a Jenkins server using terraform, and AWS EC2

Infrastructure as code

ยท

6 min read

As we know as a DevOps engineer we need to work with CI/CD i.e. continuous integration and continuous delivery and sometimes continuous deployment. we can achieve this phase of DevOps by some tools like Jenkins, GitHub Actions, CircleCI, TeamCity, etc. these tools must be installed in our infrastructure so that we can work on it.

In this blog I am going to provide each and every step needed to install Jenkins in our infrastructure using terraform. Means that we are creating an ec2 instance which will be already installed with Jenkins.

First we will going to do this step manually so that we know exactly what are the steps we need to perform.

So let's Start ๐Ÿ˜ƒ๐Ÿ˜ƒ.

Deploy an EC2 instance with Jenkins using AWS console (manually)

Here, we are going to deploy an ec2 instance in our AWS cloud graphically without terraform code.

Step 1 :- Login to AWS console.

image.png

Step 2 :- Search for ec2 and get into the section.

image.png

You will gonna see something like this

image.png

Click on instance section their.

image.png

You might be not see something like above because i just terminated my old instance that are now not useful for me.

Step 3 :- Click on Launch Instance

image.png

Step 4 :- Create an AWS instance with Cent OS image with t2.micro instance type.

image.png

Step 5 :- create a key-pair or select any if you have one.

image.png

Step 6 :- Create a security group and allow SSH traffic and TCP traffic with port 8080 open to access Jenkins with the port 8080

image.png

image.png

Note :- Here I used source type as anywhere but in real-time we must have to use My-IP so that only your IP can access the jenkins server and connect to EC2 instance via SSH.

Step 7 :- Expand Advance detail section and scroll down until you find user data section.

image.png

  • user data is used to run commands in the ec2 instance after ec2 is up and running. We can also provide bash script here so that it can run in instance.

  • so we are going to insert the bash script here that will install and run Jenkins on the server.

Bash script to install and run Jenkins.

#!/bin/sh

# updating the machine packages
sudo yum update -y 

#installing wget and git package
sudo yum install wget git -y

# Downloading the Jenkins package
sudo wget -O /etc/yum.repos.d/jenkins.repo \
    https://pkg.jenkins.io/redhat-stable/jenkins.repo

# importing the Jenkins key
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

# again upgrading the packages
sudo yum upgrade -y

# installing java 
sudo yum install java-11-openjdk -y

# installing jenkins
sudo yum install jenkins -y

# Realoading daemon
sudo systemctl daemon-reload

# Enablinng jenkins so that at every startup of machine jenkins service will start
sudo systemctl enable jenkins

# Now here starting jenkins
sudo systemctl start jenkins

Paste this script to user data section

image.png

OK now we are done we have to just launch the instance.

image.png

We will see in EC2 dashboard an ec2 instance is running

image.png

But if we just use public Ip address of this instance and connect to port 8080 we will not get any thing.

image.png

Why?? -> Because the script need some time to run we just have to wait for some time while the Jenkins will installed and run successfully. Just give some time to your server. we have done lots of work grab some tea or coffee.

Hey !! ๐Ÿ˜ƒ See Jenkins is running on our EC2 instance.

image.png

It takes 3 to 4 mins in my case may be take longer or shorter in your case.

EC2 instance with Jenkins using Terraform

We have already seen how to deploy an EC2 instance with jenkins but manually Now we are going to deploy the same thing but with using terraform.

Terraform Code

terraform{
    required_providers{
        aws = {
            source = "hashicorp/aws"
            version = "~> 4.16"
        }
    }

    required_version = ">= 1.2.0"
}

provider "aws"{
    region = "ap-south-1"
}
resource "aws_instance" "jenkins1"{
    ami = "ami-0667149a69bc2c367"
    instance_type = "t2.micro"
    key_name = "jenkins"

    user_data = <<-EOF
    #!/bin/sh

    # updating the machine packages
    sudo yum update -y 

    #installing wget and git package
    sudo yum install wget git -y

    # Downloading the Jenkins package
    sudo wget -O /etc/yum.repos.d/jenkins.repo \
        https://pkg.jenkins.io/redhat-stable/jenkins.repo

    # importing the Jenkins key
    sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

    # again upgrading the packages
    sudo yum upgrade -y

    # installing java 
    sudo yum install java-11-openjdk -y

    # installing jenkins
    sudo yum install jenkins -y

    # Realoading daemon
    sudo systemctl daemon-reload

    # Enablinng jenkins so that at every startup of machine jenkins service will start
    sudo systemctl enable jenkins

    # Now here starting jenkins
    sudo systemctl start jenkins

    EOF

    security_groups = [aws_security_group.jenkinssg2.name]

tags = {
    Name = "Jenkins server"
}
}

resource "aws_security_group" "jenkinssg2" {
    name = "jenkinssg2"
    ingress {
        from_port = 8080
        to_port = 8080
        protocol = "TCP"
        cidr_blocks = ["0.0.0.0/0"]
    }
    ingress {
        from_port = 22
        to_port = 22
        protocol = "TCP"
        cidr_blocks = ["0.0.0.0/0"]
    }

    egress {
        from_port = 0
        to_port = 0
        protocol = "-1"
        cidr_blocks = ["0.0.0.0/0"]
        ipv6_cidr_blocks = ["::/0"]
    }

}

In my previous blog of terraform. I already discussed how to apply this code and all the other steps with explanation, checkout that blog here

But Still....

Step 1 :- Open terminal and change directory to the location where terraform code is present and run command :- terraform init

image.png

Step 2 :- Now after the init command apply the terraform code using terraform apply

image.png

Step 3 :- Terraform will display what are the changes terraform is going to make and asks you for confirmation. If you are satisfied with change just pass yes

image.png

image.png

Step 4 :- Check on AWS if the changes applied or not.

image.png

  • Here we see 2 Jenkins-server are running 1st one we deployed with AWS console (manually) and other with terraform.

Here also we get the instance but the Jenkins will not run for sometime because you know the reason because Jenkins shell script is running in background and need some time to install and start Jenkins

image.png

After sometime the Jenkins will start and we will able to work with it using EC2 IP address and port port (8080)

image.png

See after 5 to 6 mins the Jenkins is up and running.

Terraform is Infrastructure as code tool not configuration management tool so to install some software or coping files from one system to another we must use configuration management tool like Ansible and chef.

Avoid installing software through terraform instead use Ansible or any other confuration management tool (It's my personal advice.)

Now that's it for this blog hope you got some knowledge through it. ๐Ÿ˜Š๐Ÿ˜Š

Thank You.

Did you find this article valuable?

Support Gautam Jha by becoming a sponsor. Any amount is appreciated!

ย