Uncategorised

Using Terraform with Proxmox

Really useful if you want to have a home setup and need to bypass the creating VM’s each time you are working on a new idea/project.

Terraform is a product by Hashicorp which makes easy work of building VM’s.

Install Terraform

I’m using a Windows 11 machine, but running WSL AlmaLinux OS 9 (so I ran it as a RHEL box). Hashicorp install options – https://developer.hashicorp.com/terraform/install




# 1. Download the official AlmaLinux 9 Generic Cloud Image
wget https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2

# 2. Create the VM (ID 9001 for Alma)
qm create 9001 --name "alma9-template" --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0

# 3. Import the disk (Replace 'local-lvm' with your storage name)
qm importdisk 9001 AlmaLinux-9-GenericCloud-latest.x86_64.qcow2 local-lvm

# 4. Attach the disk and Cloud-init drive
qm set 9001 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-9001-disk-0
qm set 9001 --ide2 local-lvm:cloudinit

# 5. Configure boot and serial console
qm set 9001 --boot c --bootdisk scsi0 --serial0 socket --vga serial0

# 6. Convert it to a template
qm template 9001


Add a user

# 1. Add the user to the PVE realm
pveum user add terraform-user@pve --password YOUR_PASSWORD

# 2. Grant Administrator permissions to the new user at the root level
pveum acl modify / --user terraform-user@pve --role Administrator

# 3. Create the API token and disable privilege separation (privsep=0)
pveum user token add terraform-user@pve prov-token --privsep 0

Open your .bashrc

Edit your .bashrc file to save these credentials

nano ~/.bashrc
#Add
# Proxmox Terraform Credentials
export PROXMOX_VE_ENDPOINT="https://192.168.1.1:"
export PROXMOX_VE_API_TOKEN="terraform-user@pve!prov-token=YOUR_TOKEN_SECRET"
export PROXMOX_VE_INSECURE="true"



Terraform code for proxmox Virtual machine

now create the terraform code, create three files first one is called versions.tf and add the following

terraform {
  required_providers {
    proxmox = {
      source  = "bpg/proxmox"
      version = "0.70.0" # Or your preferred version
    }
  }
}

Next is main.tf and add the following (to get an SSH key if you don’t have one, follow this https://www.ssh.com/academy/ssh/keygen)

resource "proxmox_virtual_environment_vm" "k3s_alma_node" {
  name      = "k3s-alma-01" # set the name of the VM here
  node_name = "prox1" # the name of your node running proxmox

  clone {
    vm_id = 9001 # The AlmaLinux template ID
  }

  initialization {
    user_account {
      # Alma uses 'almalinux' by default
      username = "tech-admin" 
      keys     = ["add your public ssh key here"]
    }

    ip_config {
      ipv4 {
        address = "dhcp"
      }
    }
  }
}

then just run

terraform init
terraform plan 
terraform apply

Next create containers with Terraform

Download a LXC Template

# Update the container database
pveam update

# See what is available
pveam available | grep almalinux-9

# Download the AlmaLinux 9 default template to 'local' storage
pveam download local almalinux-9-default_20240911_amd64.tar.xz

Terraform code for LXC container

resource "proxmox_virtual_environment_container" "my_container" {
  node_name = "YOUR_NODE_NAME" # proxmox node 
  vm_id     = 200 # Choose a unique ID

  initialization {
    hostname = "test-container" # name of the container you are creating

    ip_config {
      ipv4 {
        address = "dhcp"
      }
    }

    user_account {
      keys     = ["ssh-rsa YOUR_PUBLIC_KEY"]
      password = "YOUR_ROOT_PASSWORD"
    }
  }

  network_interface {
    name   = "eth0"
    bridge = "vmbr0"
  }

  operating_system {
    # Point to the template you downloaded in step 1
    template_file_id = "local:vztmpl/almalinux-9-default_20240911_amd64.tar.xz"
    type             = "centos"
  }

  disk {
    datastore_id = "local-lvm" # Your storage name
    size         = 8           # Size in GB
  }
}

then just run

terraform plan
terraform apply