Use this when provisioning VMs with Terraform or OpenTofu on vSphere, Proxmox, Hyper-V, libvirt or cloud, or when plan and reality disagree.
Terraform
Terraform provisions VMs declaratively with providers for vSphere, Proxmox, Hyper-V, libvirt and clouds; terraform plan shows what will change, apply makes it so, and the state file tracks what Terraform manages.
1. Terraform creates VMs declaratively via providers.
2. Remote state with locking.
3. Review plans.
4. Avoid manual changes (drift).
5. Pin provider versions.
HCL → provider → API → VMs → state
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Checks Verify boundary
terraform init
terraform validate
terraform plan -out=tfplan
Review the plan before apply; destroy plans need extra care.
Step 2 Common mistakes Narrow the fault
- Different provider versions between team members (commit
.terraform.lock.hcl). - Changes in the GUI not reflected in code.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Never edit state by hand without a backup.
- Changing some attributes forces replace — read the plan.
- Remote state needs locking.
terraform init && terraform plan— providers and planterraform state list— managed resourcesterraform providers— bpg/proxmox, vsphere, hyperv providers
ReferenceWhy it matters1 section · depth on demand
Providers
| Platform | Provider |
|---|---|
| vSphere | hashicorp/vsphere |
| Proxmox | bpg/proxmox (or telmate/proxmox) |
| libvirt | dmacvicar/libvirt |
| Hyper-V | taliesins/hyperv (community) |
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Terraform/OpenTofu provisions VMs declaratively. Example with the bpg/proxmox provider:
resource "proxmox_virtual_environment_vm" "web" {
name = "web01"
node_name = "pve1"
clone { vm_id = 9000 }
cpu { cores = 2 type = "x86-64-v3" }
memory { dedicated = 4096 }
network_device { bridge = "vmbr0" vlan_id = 20 }
initialization {
ip_config { ipv4 { address = "10.0.20.11/24" gateway = "10.0.20.1" } }
user_account { username = "ops" keys = [var.ssh_key] }
}
}
Pin provider versions, store state remotely with locking, and use lifecycle { ignore_changes = [...] } for attributes changed outside Terraform on purpose.
Example (Proxmox provider)
resource "proxmox_virtual_environment_vm" "web" {
name = "web01"
node_name = "pve1"
clone { vm_id = 9000 }
cpu { cores = 2 }
memory { dedicated = 4096 }
}
Documented example, not captured output. Run terraform plan before apply and keep state remote with locking.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Automation → Terraform. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://libvirt.org/html/index.html · confidence 94%