Modern software development requires fast and high-quality delivery of new features and fixes. CI/CD (Continuous Integration and Delivery) automation allows you to:
Jenkins and Terraform are a powerful combination of tools for CI/CD automation:
Jenkins:
Terraform:
By using Jenkins and Terraform together, you can:
/usr/local/bin/
.terraform --version
Next, we will use Terraform to create the necessary infrastructure. We recommend using at least 2 CPUs, 4 GB RAM for a basic Jenkins setup, and 15–40 GB disk for temporary files and artifacts.
Create the configuration file provider.tf
:
terraform {
required_providers {
hm = {
source = "hostman-cloud/hostman"
}
}
required_version = ">= 0.13"
}
provider "hm" {
token = "your_API_key"
}
Describe infrastructure for Jenkins deployment in main.tf
:
data "hm_configurator" "example_configurator" {
location = "us-2"
}
data "hm_os" "example_os" {
name = "ubuntu"
version = "22.04"
}
resource "hm_ssh_key" "jenkins_key" {
name = "jenkins-ssh-key"
body = file("~/.ssh/id_rsa.pub")
}
resource "hm_vpc" "jenkins_vpc" {
name = "jenkins-vpc"
description = "VPC for Jenkins infrastructure"
subnet_v4 = "192.168.0.0/24"
location = "us-2"
}
resource "hm_server" "jenkins_server" {
name = "Jenkins-Server"
os_id = data.hm_os.example_os.id
ssh_keys_ids = [hm_ssh_key.jenkins_key.id]
configuration {
configurator_id = data.hm_configurator.example_configurator.id
disk = 15360
cpu = 2
ram = 4096
}
local_network {
id = hm_vpc.jenkins_vpc.id
ip = "192.168.0.10" # Static IP within the VPC subnet
mode = "dnat_and_snat"
}
connection {
type = "ssh"
user = "root"
private_key = file("~/.ssh/id_rsa")
host = self.networks[0].ips[0].ip # Correct way to get IP
timeout = "10m"
}
provisioner "remote-exec" {
inline = [
"apt-get update -y",
"apt-get install -y ca-certificates software-properties-common",
"add-apt-repository -y ppa:openjdk-r/ppa",
"apt-get update -y",
"apt-get install -y openjdk-17-jdk",
"curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null",
"echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | tee /etc/apt/sources.list.d/jenkins.list > /dev/null",
"apt-get update -y",
"apt-get install -y jenkins",
"update-alternatives --set java /usr/lib/jvm/java-17-openjdk-amd64/bin/java",
"sed -i 's|JAVA_HOME=.*|JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64|' /etc/default/jenkins",
"systemctl daemon-reload",
"systemctl start jenkins",
"ufw allow 8080"
]
}
}
output "jenkins_url" {
value = "http://${hm_server.jenkins_server.networks[0].ips[0].ip}:8080"
}
Install the Hostman Terraform Provider:
terraform init
Check the connection to Hostman:
terraform plan
After successful initialization, you should see the following message:
Apply the changes:
terraform apply
Confirm execution by typing yes
.
When you go to your Hostman project, you will see a new server being created. Take note of the resources—they match exactly what we specified in the main.tf
configuration.
After the server is created, go to the IP address with port 8080, where Jenkins should now be running. This address is also visible in the control panel, on the server Dashboard.
You will see Jenkins’ initial setup screen:
In this section, we will go through each part of the code in detail.
Fetching the configurator ID for the selected region. Needed to define available hardware configurations (CPU/RAM/Disk):
data "hm_configurator" "example_configurator" {
location = "us-2"
}
Fetching the OS image ID for Ubuntu 22.04.
data "hm_os" "example_os" {
name = "ubuntu"
version = "22.04"
}
Creating an SSH key for server access:
resource "hm_ssh_key" "jenkins_key" {
name = "jenkins-ssh-key"
body = file("~/.ssh/id_rsa.pub")
}
Creating a private network (VPC) for Jenkins isolation:
resource "hm_vpc" "jenkins_vpc" {
name = "jenkins-vpc"
description = "VPC for Jenkins infrastructure"
subnet_v4 = "192.168.0.0/24"
location = "us-2"
}
Main server parameters:
name
: Server nameos_id
: OS image IDssh_keys_ids
: SSH key bindingresource "hm_server" "jenkins_server" {
name = "Jenkins-Server"
os_id = data.hm_os.example_os.id
ssh_keys_ids = [hm_ssh_key.jenkins_key.id]
Server characteristics:
configurator_id
: Link to configuratordisk
: Disk size (15 GB)cpu
: Number of coresram
: Memory size (4 GB)configuration {
configurator_id = data.hm_configurator.example_configurator.id
disk = 15360
cpu = 2
ram = 4096
}
Private network settings:
id
: ID of created VPCip
: Static IP within subnetmode
: NAT modelocal_network {
id = hm_vpc.jenkins_vpc.id
ip = "192.168.0.10"
mode = "dnat_and_snat"
}
SSH connection parameters for provisioning:
connection {
type = "ssh"
user = "root"
private_key = file("~/.ssh/id_rsa")
host = self.networks[0].ips[0].ip
timeout = "10m"
}
provisioner "remote-exec" {
inline = [
# Package update
"apt-get update -y",
# Installing dependencies
"apt-get install -y ca-certificates software-properties-common",
# Adding Java repository
"add-apt-repository -y ppa:openjdk-r/ppa",
# Installing Java 17
"apt-get install -y openjdk-17-jdk",
# Adding Jenkins repository
"curl -fsSL ...",
"echo deb ...",
# Installing Jenkins
"apt-get install -y jenkins",
# Java configuration
"update-alternatives --set java ...",
"sed -i 's|JAVA_HOME=.*|...|' /etc/default/jenkins",
# Starting service
"systemctl daemon-reload",
"systemctl start jenkins",
# Opening port
"ufw allow 8080"
]
}
Displaying the URL for accessing Jenkins:
output "jenkins_url" {
value = "http://${hm_server.jenkins_server.networks[0].ips[0].ip}:8080"
}
Problem: Storing API keys, SSH keys, and other secrets may be unsafe if added as plain text.
Solution: Use secret managers such as HashiCorp Vault, or Jenkins integration with credential management systems. In Terraform, specify secrets via environment variables or remote storage.
Problem: The state file (terraform.tfstate
) may be corrupted or overwritten if multiple users access it simultaneously.
Solution: Configure a remote backend (e.g., object storage) to store the state. Enable state locking to prevent simultaneous command execution.
Problem: Integration can be difficult for beginners due to differences in configuration and infrastructure management.
Solution: Use the Terraform plugin for Jenkins to simplify setup. Create detailed documentation and use Jenkinsfile templates for repeatable processes.
Problem: New versions of Jenkins and Terraform may be incompatible with existing configurations.
Solution: Test updates in local or test environments. Regularly update Jenkins plugins to maintain compatibility.
Problem: Not all actions or resources may be available through the API.
Solution: Use a combination of Terraform and the Hostman interface. Leave operations unavailable via API for manual execution or scripts in Python/Go.
Problem: Limits on the number of servers, storage, or networking resources may cause scaling delays.
Solution: Optimize existing resource usage. Plan workload ahead, increasing limits through Hostman support.
Problem: Deploying large applications may be difficult due to complex network configurations (e.g., VPC).
Solution: Break infrastructure into Terraform modules. Use pre-prepared network architecture templates.
Problem: Hostman may not provide full integrations with external services, such as GitHub Actions or third-party monitoring.
Solution: Configure webhook notifications for integration with Jenkins or other CI/CD systems. Use external APIs of third-party services for custom integrations.
Use the terraform plan
option to validate configuration before applying changes.
For detailed analysis, add the -debug
flag:terraform apply -debug
Verify provider settings (e.g., token and API keys).
--verbose
or --debug
in Terraform and Docker build commands.Efficient work with Jenkins, Terraform, and Hostman requires awareness of possible issues and systematic resolution. Use the recommendations above to prevent errors, optimize resources, and simplify debugging of your CI/CD processes.
For Beginners:
init
, plan
, apply
. Configure Jenkins using pre-built images and plugins.For Professionals:
Jenkins and Terraform combined with Hostman open wide opportunities for developers and DevOps engineers. Beginners can easily learn these tools by starting with simple projects, while professionals can implement complex CI/CD pipelines with multi-cloud infrastructure. This approach not only accelerates development but also helps create a scalable, secure, and fault-tolerant environment for modern applications.
Terraform Documentation:
Jenkins Guides:
Hostman:
Useful Tools:
Communities: