Konstantinos Antoniou.

← Projects

Automating Proxmox Virtual Machine Deployment with Terraform

Overview

As part of my DevOps learning journey, I explored Infrastructure as Code (IaC) using Terraform to automate the deployment and management of virtual machines in my Proxmox home lab. The goal was to move away from manual VM creation through the Proxmox web interface and build a fully automated, scalable system capable of provisioning, configuring, and destroying VMs on demand — a foundational step toward automating Kubernetes cluster scaling and reproducible environments.


Learning Terraform

I began by studying Terraform’s core concepts:

  • Providers: Plugins that allow Terraform to interact with infrastructure platforms (in this case, Proxmox).
  • Resources: The building blocks that define what Terraform creates — such as VMs, networks, or templates.
  • State Management: How Terraform tracks deployed resources using a local or remote terraform.tfstate file.
  • Variables and Outputs: For reusable configurations and parameterized deployments.
  • Modules: For organizing reusable code blocks.

After understanding the syntax of HCL (HashiCorp Configuration Language), I practiced basic deployments on cloud providers like AWS, then transitioned to my own on-premises Proxmox server, which offered better control and visibility of the provisioning process.


Proxmox Setup

My lab consists of:

Before Terraform could communicate with Proxmox, I created an API token via the Proxmox web UI:

Datacenter → Permissions → API Tokens → Add

This token allowed Terraform to authenticate and manage resources securely.


Terraform Configuration

I used the bpg/proxmox provider, which is currently the most maintained and feature-rich Terraform provider for Proxmox.

Provider Block (providers.tf):

terraform {
  required_providers {
    proxmox = {
      source  = "Telmate/proxmox"
      version = "3.0.2-rc05"
    }
  }
}

provider "proxmox" {
  pm_api_url          = "https://192.0.2.20:8006/api2/json"
  pm_api_token_id     = var.pm_api_token_id
  pm_api_token_secret = var.pm_api_token_secret
  pm_tls_insecure     = true
}

Variable Definitions

To make the configuration reusable, I declared variables for VM name, CPU, memory, and network settings.

variables.tf:

variable "pm_api_token_id"{
  default = "Terraform@pam!Terraform"
}
variable "pm_api_token_secret"{
  default = "Token_Generated_From_ProxMox"  
}

variable "vm_count" {
 default = 1
}

variable "vmid" {
  default = 9000
}

variable "base_name" {
  default = "Terraform"
}

variable "vm_template_id" {
  default = 1300
}

variable "target_node" {
  default = "pve_hostname"
}

variable "ciuser" {
  default = "user"
}

variable "cipassword" {
  default = "password"
}

variable "ipconfig" {
  default = "ip=dhcp"
}

#variable "ipconfig1" {
#  default = "ip=192.0.2.${60 + count.index}/25,gw=192.0.2.1"
#}

Main Configuration

The main Terraform file defines how each VM is cloned from the base template and customized via cloud-init.

main.tf:

resource "proxmox_vm_qemu""My_ProxMox" {
  count       = var.vm_count
  vmid        = var.vmid + count.index
  onboot      = true
  vm_state    = "running"
  agent       = 1
  name        = "${var.base_name}-${count.index + 1}"
  target_node = var.target_node
  clone_id    = var.vm_template_id
  full_clone  = true
  memory      = 1024
  scsihw      = "virtio-scsi-single"
  boot        = "order=scsi1;ide2;net0"
  os_type     =  "cloud-init"
  ciuser       = var.ciuser
  cipassword   = var.cipassword
  ciupgrade    = true
  sshkeys   = file("./ssh.keys")

  ipconfig0  = var.ipconfig
  searchdomain = "local"
  nameserver   = "8.8.8.8"      

  # CPU configuration
  cpu {
    cores   = 1
    sockets = 1
    type    = "host"
  }

  # Serial Block
  serial {
    id = 0
    type = "socket"
  }

  # VGA configuration
  vga {
    type = "serial0"
  }
  
  # Disk configuration
  disk {
    slot     = "scsi1"
    type     = "disk"
    storage  = "VMs"
    size     = "63G"
  }

  # cloudinit disk
  disk {
  slot    = "ide2"
  type    = "cloudinit"
  storage = "VMs"
  size    = "4G"  # size of the Cloud-Init drive
 }

  # Network adapter
  network {
    id    = 0
    model = "virtio"
    bridge = "vmbr0"
  }

}

Each VM receives a dynamic name (terraform-1, terraform-2, etc.) and automatically boots with a DHCP-assigned IP. Cloud-init injects the SSH key and username, allowing passwordless access after provisioning.


Outputs

I used outputs to display the newly created VM names and Proxmox IDs after each run.

outputs.tf:

output "vm_names" {
  value = [for vm in proxmox_vm_qemu.My_ProxMox : vm.name]
}

output "vm_ids" {
  value = [for vm in proxmox_vm_qemu.My_ProxMox : vm.vmid]
}

After a successful deployment, Terraform prints a clean summary of the created VMs.


Execution Steps

1. Initialize Terraform:

terraform init

Screenshot 1: Execution Steps

2. Preview the plan:

terraform plan

This step shows the exact actions Terraform will take without executing them.

Screenshot 2: Execution Steps

Screenshot 3: Execution Steps

3. Apply the configuration:

terraform apply -auto-approve

Screenshot 4: Execution Steps

Terraform clones the template and provisions all 4 VMs automatically on Proxmox.

4. Check in Proxmox UI:

Each VM appears under the assigned node, booted, and ready to connect via SSH.

Screenshot 5: Execution Steps

Screenshot 6: Execution Steps

Screenshot 7: Execution Steps

5. Destroy when done:

terraform destroy -auto-approve

This removes all created VMs, leaving the environment clean.

Screenshot 8: Execution Steps

Screenshot 9: Execution Steps


Scaling and Kubernetes Use Case

This automated deployment forms the backbone for dynamic scaling of my Kubernetes cluster. By adjusting the vm_count variable, I can:

  • Increase the number of worker nodes in seconds
  • Ensure consistent VM specs
  • Automate cluster expansion through Terraform + Ansible integration

Terraform acts as the infrastructure layer, while Ansible handles post-provisioning configuration (such as installing Docker and joining nodes to the cluster).


Challenges and Lessons Learned

  • Template IP conflicts: Initially, all cloned VMs had no ip provisioning because during the creation of the template i hadn’t cleaned the machine-id and cloud-init configuration, so every new clone had the same MAC address and couldn’t assign an ip neither with DHCP nor STATIC configuration. The trick was to clear the machine id and after that everything worked flawlessly.
  • Provider limitations: Some Terraform Proxmox providers were outdated or unstable like bpg/proxmox. The Telmate/proxmox provider offered reliable cloud-init support.
  • State management: Understanding how Terraform tracks changes was essential to prevent orphaned or duplicated VMs.

Through this process, I learned how to codify infrastructure, manage environments declaratively, and automate repetitive administrative work.


Conclusion

Using Terraform to automate VM provisioning on Proxmox transformed how I manage my home lab. Instead of manually creating, configuring, and deleting virtual machines, I now rely on a single command to reproduce entire environments consistently. This project not only deepened my understanding of Infrastructure as Code but also laid the foundation for advanced automation — including Ansible provisioning and Kubernetes scaling.


Tech Stack