Create virtual machine in VMware using terraform

Everton Araújo
1 min readJan 22, 2023

--

Create simple vm using VMware VCenter

To create a virtual machine in VMware using Terraform, you will need to follow these steps:

  1. Install Terraform on your local machine if you do not already have it.
  2. Install the VMware vSphere provider for Terraform by following the instructions at https://registry.terraform.io/providers/vmware/vsphere/latest/docs/guides/installation
  3. Create a new directory for your Terraform configuration and navigate to it.
  4. Create a file called main.tf in this directory and add the following code:
provider "vsphere" {
user = "your-username"
password = "your-password"
vsphere_server = "vcenter.example.com"
}

resource "vsphere_virtual_machine" "vm" {
name = "vmname"
resource_pool_id = "resgroup-12345"
datacenter_id = "datacenter-12345"

num_cpus = 2
memory = 2048

network_interface {
label = "Network Adapter 1"
ipv4_address = "192.168.1.10"
ipv4_prefix_length = 24
ipv4_gateway = "192.168.1.1"
}

disk {
label = "disk0"
size = 20
}

cdrom {
datastore_id = "datastore-12345"
path = "[datastore1] ISO/vmname.iso"
}
}
  1. Replace your-username, your-password, vcenter.example.com, resgroup-12345, datacenter-12345, and datastore-12345 with the appropriate values for your environment.
  2. Run the following command to initialize Terraform:
terraform init
  1. Run the following command to create the virtual machine:

Terraform plan

Terraform apply

Finish.

--

--

Everton Araújo
Everton Araújo

No responses yet