Austin Barnes
April 14, 2022

Terraform - Proxmox Virtual Machine Deploy

Posted on April 14, 2022  •  3 minutes  • 555 words


Using Terraform to deploy virtual machines in Proxmox. This is designed with Proxmox Virtual Environment version 7.1 in mind.

Check out all of the configuration files on GitHub (proxmox-deploy-vm) at the repository!


I have a Proxmox server in my homelab, and wanted to have an easier way to spin up virtual machines on an as needed basis.


  1. Have a Proxmox server
  2. Have a template made on Proxmox, in my case, I used Ubuntu Server 20.04.
  3. Have Terraform installed


  1. Create service user in Proxmox with appropriate settings for Terraform deployments
  2. Configure Terraform files & variables for deployment
  3. Deploy Terraform

1. Configure/Create Terraform User & Role

This can be also be done under Datacenter–>“Proxmox server” –> Permissions –> Users & Roles in the GUI.

Create user and assign it a role

  1. SSH into the Proxmox server
  2. Create the role in Proxmox with appropriate permissions
    • pveum role add TerraformProv -privs "VM.Allocate VM.Clone VM.Config.CDROM VM.Config.CPU VM.Config.Cloudinit VM.Config.Disk VM.Config.HWType VM.Config.Memory VM.Config.Network VM.Config.Options VM.Monitor VM.Audit VM.PowerMgmt Datastore.AllocateSpace Datastore.Audit VM.Console"
  3. Create the user
  4. Assign user to the role
  5. OPTIONAL: Create a token tom use rather than username and password

2. Configure Terraform files

This is broken down into 4 files.,,, and

Create file

This file will host Terraform provider information and versioning, along with the connection parameters to your Proxmox server. Put the following into it:

terraform {
  required_providers {
    proxmox = {
      source = "Telmate/proxmox"
      version = ">=2.9.6"

provider "proxmox" {
  pm_api_url = var.PM_URL
  pm_user    = var.PM_USER
  pm_password    = var.PM_PASS

Create file

Contains code to deploy the Virtual Machine.

#  Setup Cloud-init w/
/* Uses Cloud-Init options from Proxmox 5.2 */
resource "proxmox_vm_qemu" "cloudinit-test" {
  name        = var.linux_fqdn
  desc        = "terraform deploy"
  target_node = var.PM_node

  clone = var.PM_template

  # The destination resource pool for the new VM
  pool = ""

  storage = "datastore"
  cores   = 1
  sockets = 1
  memory  = 2048
  disk_gb = 8
  nic     = "virtio"
  bridge  = "vmbr0"

  ssh_user        = "root"
  ssh_private_key = var.ssh_priv

  os_type   = "cloud-init"
  ipconfig0 = "ip=${var.linux_ip}/${var.linux_subnetmask},gw=${var.linux_gateway}"

  sshkeys = var.ssh_keys

  provisioner "remote-exec" {
    inline = [
      "ip a"

Create file

Variables declared here, which are defined in the .tfvars file.

variable "PM_USER" {} 
variable "PM_PASS" {} 
variable "PM_URL" {}
variable "PM_node" {} 
variable "PM_template" {}
variable "ssh_keys" {}
variable "ssh_priv" {}
variable "ssh_user" {} 
variable "linux_fqdn" {}
variable "linux_ip" {}
variable "linux_subnetmask" {}
variable "linux_gateway" {}

Create file

Put your custom defined variables into this file.

# Example .tfvars file
PM_PASS = "proxmox terraform user password"
PM_USER = "proxmox terraform user username"
PM_URL = "https://IPADDRESSorFQDN:8006/api2/json"
PM_node = "target proxmox server name"
PM_template = "target tempalte in Proxmox"
linux_fqdn = "desired name.domain"
linux_ip = "desired IP"
linux_subnetmask = "subnet mask of network" # Should be 24, 26, 30, etc.
linux_gateway = "gateway ip"

ssh_user = "linux username" 

ssh_keys = <<EOF
ssh-rsa key here
ssh-rsa key here

ssh_priv = <<EOF
private keye here

3. Deploy terraform

  1. Navigate to directory, run terraform init
  2. terraform apply --auto-approve

Check Proxmox server to see VM

  1. Destory it with terraform destroy --auto-approve

Useful Resources

Follow me

I put a little bit of everything on here, so follow along in my journey if you wish