Terraform for VMware vSphere
Automate VM deployments on vSphere using Terraform — from installing the provider to deploying fleets of VMs with custom sizes, IPs, and host targeting. Infrastructure as Code, lab-proven.
About This Guide
main.tf code provided and screenshots from production deployments.
Getting Started
Install Terraform on Windows
Download from the Terraform Downloads Page, extract to C:\Terraform, add to system PATH, then verify with terraform --version.
Prepare Your Configuration Files
Create a project folder (e.g. C:\MyTerraformProject), write your main.tf using VS Code or Notepad++, then navigate to the folder in PowerShell.
Initialize & Apply
Run terraform init to download the vSphere provider, then terraform plan to preview, and terraform apply to deploy. That’s it.
Lab Walkthroughs
Create 1 Linux VM from a Template
BeginnerConnect to vCenter, load your datacenter, cluster, datastore, and network objects, then clone a Linux template into a new VM named Linux1 with a static IP, custom hostname, and EFI firmware.
name = “Linux1”
num_cpus = 2
memory = 1024 # 1 GB
firmware = “efi”
…
}
Deploy 10 VMs with Custom IP Range
IntermediateUse Terraform’s count meta-argument to loop and deploy multiple VMs automatically — each with a sequentially assigned static IP address from a defined range.
ipv4_address = “192.168.221.${20 + count.index}”
# Produces: Linux-1 → .20, Linux-2 → .21 …
Deploy 5 VMs on a Specific ESXi Host
IntermediateTarget a specific ESXi host (h2.abdelwahed.me) instead of the cluster resource pool — useful for host-pinning, licensing control, or maintenance scenarios.
name = “h2.abdelwahed.me”
}
resource_pool_id = data.vsphere_host.host_h2.resource_pool_id
Deploy VMs with Different Sizes
AdvancedUse a Terraform variable map to define heterogeneous VM configurations — deploy a web server, database, and test VM each with different CPU, memory, and IP in a single terraform apply.
default = [
{ name = “web”, cpu = 2, mem = 2048 },
{ name = “db”, cpu = 4, mem = 4096 },
{ name = “test”, cpu = 1, mem = 1024 },
]
}
Who Is This For
VMware Admins
vSphere engineers wanting to automate repetitive VM provisioning tasks.
DevOps Engineers
Teams adopting Infrastructure as Code for on-premises VMware environments.
Cloud Architects
Professionals bridging traditional vSphere to hybrid or multi-cloud workflows.
Guide Details
Download the Complete Guide
Full Terraform code, step-by-step labs, and production screenshots — free to download.