Set Up Terraform in your Jenkins Docker Container
Tags: jenkins, terraform, docker, containers
Published: 2023-02-04 09:32:30

Organizations can automate their infrastructure as code (IaC) and streamline deployment processes by configuring a continuous integration and continuous development (CI/CD) pipeline to integrate Terraform  within their Jenkins Docker container.

### Terms:

**Jenkins** is an automation server widely used to automate various aspects of software development, including building, testing, and deploying applications.

**Docker** is an open-source platform that enables developers to build, deploy, run, update and manage applications in a container.

**Containers** are isolated environments that package an application and its dependencies together, allowing it to run consistently across different computing environments.

**Terraform** is a declarative open-source “Infrastructure as Code” tool, created by HashiCorp that enables the definition of on-prem and cloud resources in human-readable configuration files that can be versioned, reused, and shared as code.

In this artcle, we are going to set up terraform in a jenkins server that runs on a docker container. Let's get into it 🧑🏽‍💻

### Step 1: Pull and start the Jenkins Docker container:

The Jenkins server used in this project was installed using the documentation. [See more](https://github.com/jenkinsci/docker/blob/master/README.md)

```bash
docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 --restart=on-failure jenkins/jenkins:lts-jdk11
```

### Step 2: Install Terraform on your Docker Container

To install terraform on the container, find your Jenkins container ID and log into the container as root user:

```bash
docker ps
docker exec -u 0 -it <container_id> /bin/bash
```

Change Directory to /usr/bin directory

```bash
cd /usr/bin
```

As the root user, install terraform in the docker container: Reference the [official documentation](https://developer.hashicorp.com/terraform/downloads) for the installation guide. 

```bash
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
```

<aside>
💡 If `sudo` and `wget` above are not found, they can be installed by running the below commands:
</aside>

```bash
apt-get install sudo
apt-get install wget
```

Confirm that terraform has been installed by running the command to view its version: 

```bash
terraform --version
```

### Step 3: Install Terraform Plugin

From your **Jenkins dashboard** navigate to **Manage Jenkins >> Manage Plugins** and select the **Available** tab. Locate this plugin by searching for **Terraform** and clicking on install.


![Terraform-plugin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bry2sdlz6osv6q8rnuf0.png)

After installation of this plugin, ensure that it is enabled.

### Step 4: Configure Terraform Path

Navigate to Jenkins UI and update the path to the terraform installation path:

Dashboard >> Manage Jenkins >> Global Tool Configuration >> Scroll down to Terraform and click on **Terraform Installations.**

Type in the installation path >> uncheck the install automatically >> apply changes and save.

 
![Terraform-Installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkzy5bokgrwhdg0h9931.png)

That's it! You are now ready to write a CI/CD pipeline script to integrate terraform to automate infrastructure as code (IaC) in your Jenkins Docker container.

💡 This article was first published at [blog.paschalogu.com](https://blog.paschalogu.com). If you found it useful, follow me on [Twitter](https://twitter.com/Paschal_ik) and also on [LinkedIn](https://www.linkedin.com/in/paschal-ogu).