Manage VMs and snapshots on vSphere
Terraform, HashiCorp's infrastructure as code (IaC) offering, provides a consistent workflow for provisioning and maintaining infrastructure and services. Terraform allows organizations to take a programmatic approach to infrastructure management. By logging, auditing, and versioning infrastructure changes, organizations gain better insight into the current state of their infrastructure.
VMware is a key component of many organizations' on-premises and private cloud infrastructure. The vSphere provider enables operators to adopt Terraform to build, change, and manage common VMware resources.
In this tutorial, you will create a vSphere template using Packer and provision a virtual machine (VM) from that template using Terraform. You will then use Terraform to modify the VM's name and resource allocations and create a snapshot of the VM.
Prerequisites
This tutorial assumes you are familiar with the standard Terraform workflow. If you are unfamiliar with Terraform, complete the Get Started tutorials first.
For this tutorial, you will need:
- Terraform CLI installed locally
- Packer 1.6.6+
- govc installed locally
- Docker Desktop installed and running
- vcsim vCenter API simulator
VMware's vcsim is an open-source vCenter and ESXi API simulator. If you do not have a vSphere environment, vcsim can simulate virtual machines in the vSphere API and back them with Docker containers. Because vcsim is an API simulator, there are some key differences when compared to vSphere and some features are not supported. This tutorial reviews those differences.
To run vcsim, first clone the vmware/govmomi repository, which contains the vcsim source code.
Navigate to the vcsim
directory in your terminal.
Build the vcsim binary.
Start vcsim.
Clone repository
Clone the example repository.
Navigate to the repository directory in your terminal.
This directory contains the Packer files you need to create an Ubuntu template image and an initial Terraform configuration to provision a virtual machine in vcsim.
Create a vSphere template using Packer
Navigate to the packer
directory.
This directory contains four files.
- The
vsphere-iso_basic_ubuntu.pkr.hcl
file is the base Packer template. It uses thevsphere-iso
builder to create an Ubuntu 22.04.3 server image namedtf-edu-ubuntu
. - The
variables.pkr.hcl
file contains variable declarations for the following variables:vsphere_server
,vsphere_user
,vsphere_password
,datacenter
,cluster
,datastore
, andnetwork_name
. - The
vars.auto.pkrvars.hcl.example
file contains sample variable definitions forvsphere-iso_basic_ubuntu.pkr.hcl
. - The
user-data
file is automated server install configuration that installs and configures Ubuntu on your VM. Notice how the user-data file installsopen-vm-tools
. Packer uses the VMware tool set to extract the IP from the VM.
Copy the contents of vars.auto.pkrvars.hcl.example
into a new file named vars.auto.pkrvars.hcl
.
Open vars.auto.pkrvars.hcl
in your text editor. The default values of this file configure Packer to connect to vcsim.
Next, open the vsphere-iso_basic_ubuntu.pkr.hcl
file and uncomment the configuration_parameters
block. This block configures the Docker container that will back your simulated virtual machine and is only required when using vcsim.
Delete the boot_command
option, as vcsim does not currently support this feature. This option specifies the keys to type when the virtual machine first boots in order to start the OS installer. Since vcsim will use a Docker container to simulate your VM, this option is not required for this tutorial.
Next, initialize the Packer configuration.
Finally, build the Ubuntu template to your vSphere cluster.
Verify that Packer successfully created the template by using govc
.
Explore configuration and define variables
Now that you have created the template, you are ready to provision a VM with Terraform using that template.
Return to the repository's root directory containing the Terraform configuration you will use to provision your VM on vSphere.
Here you will find main.tf
, variables.tf
, terraform.example.tfvars
, and versions.tf
.
Explore main.tf
Open main.tf
. This file uses the vSphere provider to deploy a virtual machine from your newly created Ubuntu template. This file contains four main sections.
The
vsphere
provider block contains the connection information Terraform uses to authenticate and connect to the vSphere API.Terraform uses the
vsphere_virtual_machine
data source to retrieve information about the Ubuntu template that you created with Packer. The data includes the data center ID, data store ID, cluster ID, network ID, and the Ubuntu template ID. Terraform uses these values to provision the virtual machine.The
vsphere_virtual_machine
resource defines the configuration that Terraform uses to provision the virtual machine. Notice how this resource references the previously defined data sources (for example:data.vsphere_compute_cluster.cluster.resource_pool_id
). By using data sources to retrieve these IDs instead of hardcoding them, the configuration is more user-friendly, comprehensible, and robust.Tip
The Terraform Registry contains provider-specific documentation. The
vsphere_virtual_machine
Registry page includes a full list of arguments, attributes, and example configurations that you can reference when customizing your provider.
Define variables
Open variables.tf
. This file contains the input variables this configuration uses. Because the values of these variables depend on your specific configuration, you will need to customize them to your cluster.
Copy the contents of terraform.tfvars.example
into a new file named terraform.tfvars
.
Open terraform.tfvars
. The default values of this file configure Terraform to connect to vcsim.
Warning
This file contains sensitive information used to connect to your cluster. You should never be commit sensitive values into source control. The .gitignore
file found in this repo ignores all .tfvars
files. You should include this file in any of your future Terraform repos.
Provision a VM from template
In your terminal, initialize your Terraform workspace.
Apply your configuration. Remember to confirm your apply with a yes
.
Verify that Terraform provisioned the VM successfully by using govc
.
Modify VM
Now that you have provisioned the VM, modify the configuration in main.tf
to double the memory and change the name to to learn-terraform-doubled
.
Apply your configuration to update your VM. Remember to confirm your apply with a yes
.
Verify that Terraform provisioned the VM successfully by using govc
.
Create a snapshot
Next, you will create a snapshot of the VM. Add the following resource to your main.tf
.
Notice how the vsphere_virtual_machine_snapshot
references the VM you provisioned earlier in virtual_machine_uuid
.
Apply your configuration to create your snapshot. Remember to confirm your apply with a yes
.
Verify that Terraform created the snapshot successfully by using govc
.
You have successfully created a VM and snapshot in vSphere using Terraform.
Clean up your infrastructure
Destroy the resources you created when you finish this tutorial. Remember to respond to the confirmation prompt with yes
.
Next steps
In this tutorial, you created a template in vSphere using Packer then cloned and modified a virtual machine in vSphere using Terraform. In addition, you created a snapshot.
To learn more about managing vSphere resources with Terraform, including how to create modules and use the vSphere provider, visit the following resources:
- The Terraform vSphere Provider Registry page
- See a sample configuration of a VM deployment from an OVF/OVA template
- The
vsphere-iso
Packer documentation - Reuse Configuration with Modules tutorials
- List of Terraform vSphere modules