Back in May I used Terraform to create some containers in my Proxmox environment.

Adjustments to Proxmox

Since my original post I have turned my single Proxmox node into a Proxmox cluster, added some shared storage and created a cloudinit template. With all of that in place I’m now going to look at deploying a virtual machine (VM) from the cloudinit template using Terraform.

Terraform

The terraform for deploying from a cloudinit is available on the registry for Telmate/Proxmox, but I’m also providing my alterations. As with my container terraform I’m using a variable file to store my variables, its the same variable file from my container blog post but with a few added lines:

Variable File

proxmox_api_url = ""
proxmox_api_token_id = ""
proxmox_api_token_secret = ""
proxmox_container_password = ""
proxmox_container_password = ""

# Added for Cloudinit VM deployment
ssh_key = ""
ci_user = ""
ci_password = ""
name_server = ""

Terraform File for Cloudinit

The terraform file is then using some of the above variables.

variable ci_user {
    type = string
}

variable ci_password {
    type = string
}

variable ssh_key {
    type = string
}

variable name_server {
    type = string
}


resource "proxmox_vm_qemu" "cloudinit-clone" {
  vmid        = 201
  name        = "cloudinit-clone"
  target_node = "containers"
  agent       = 1
  cores       = 2
  memory      = 2048
  boot        = "order=scsi0" # has to be the same as the OS disk of the template
  clone       = "VM 9000" # The name of the template
  scsihw      = "virtio-scsi-single"
  vm_state    = "running"
  automatic_reboot = true

  # Cloud-Init configuration
  nameserver = var.name_server
  ipconfig0  = "ip=dhcp,ip6=dhcp"
  skip_ipv6  = true
  ciuser     = var.ci_user
  cipassword = var.ci_password
  sshkeys    = var.ssh_key


  serial {
    id = 0
  }

  disks {
    scsi {
      scsi0 {
        disk {
          storage = "pi"
          size    = "10G" 
        }
      }
    }
    ide {
      ide1 {
        cloudinit {
          storage = "pi"
        }
      }
    }
  }

  network {
    id = 0
    bridge = "vmbr0"
    model  = "rtl8139"
  }
}

My template is called “VM 9000”, note it is the name of the VM template and not the ID of the VM template.

The storage options should match a storage in use within your Proxmox environment (e.g. local). My shared storage is called “pi” which is why it’s in use here.

Provider File

I’ve not made any changes to the provider file since my terraform container post, but he it is for ease of reading:

terraform {
    required_providers {
        proxmox = {
            source = "telmate/proxmox"
            version = "3.0.1-rc8"
        }
    }
}

variable proxmox_api_url {
    type = string
}

variable proxmox_api_token_id {
    type = string
}

variable proxmox_api_token_secret {
    type = string
}

provider "proxmox" {
    pm_api_url = var.proxmox_api_url
    pm_api_token_id = var.proxmox_api_token_id
    pm_api_token_secret = var.proxmox_api_token_secret
    pm_tls_insecure = true
}

Deployment Speed

On my little Proxmox cluster (which is just a little homelab) it takes terraform about 10 minutes to deploy the VM. Slightly faster than manually cloning, and destroying / redeploying can be done with a few command lines.

QEMU Agent (i.e. Install it)

One thing I have not done is install the QEMU agent during the Terraform process. The QEMU agent does a few important things such as feeding back details (e.g. IP address) to the Proxmox console and handling shutdowns cleanly for the operating system. The install of the QEMU agent can be done via a snippet, but I’m using Ansible instead.