Create virtual machine in VMware using terraform
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:
- Install Terraform on your local machine if you do not already have it.
- Install the VMware vSphere provider for Terraform by following the instructions at https://registry.terraform.io/providers/vmware/vsphere/latest/docs/guides/installation
- Create a new directory for your Terraform configuration and navigate to it.
- 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"
}
}
- Replace
your-username
,your-password
,vcenter.example.com
,resgroup-12345
,datacenter-12345
, anddatastore-12345
with the appropriate values for your environment. - Run the following command to initialize Terraform:
terraform init
- Run the following command to create the virtual machine:
Terraform plan
Terraform apply
Finish.