Overview
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!
Purpose
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.
Prequisites
- Have a Proxmox server
- Have a template made on Proxmox, in my case, I used Ubuntu Server 20.04.
- Have Terraform installed
Steps
- Create service user in Proxmox with appropriate settings for Terraform deployments
- Configure Terraform files & variables for deployment
- 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
- SSH into the Proxmox server
- 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"
- Create the user
pveum user add terraform-prov@pve --password <password>
- Assign user to the role
pveum aclmod / -user terraform-prov@pve -role TerraformProv
- OPTIONAL: Create a token tom use rather than username and password
pveum user token add terraform-prov@pve terraform-token --privsep=0
2. Configure Terraform files
This is broken down into 4 files. main.tf, linuxvm.tf, variables.tf, and variables.auto.tfvars.
Create main.tf file
This file will host Terraform provider information and versioning, along with the connection parameters to your Proxmox server. Put the following into it:
|
|
Create linuxvm.tf file
Contains code to deploy the Virtual Machine.
|
|
Create variables.tf file
Variables declared here, which are defined in the .tfvars file.
|
|
Create variables.auto.tfvars file
Put your custom defined variables into this file.
|
|
3. Deploy terraform
- Navigate to directory, run
terraform init
terraform apply --auto-approve
Check Proxmox server to see VM
- Destory it with
terraform destroy --auto-approve