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+
- An existing vSphere environment
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 on your vSphere cluster.
If you already have a VM in vSphere that you want to use as a template, skip to the Provision a VM from template step.
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
.
Update vars.auto.pkrvars.hcl
with your vSphere cluster connection information.
Warning
Do not commit sensitive values into version control. The .gitignore
located in the root directory of the repo includes *.pkrvars.hcl
. It also includes **/packer_cache/*
so you don't commit sensitive values or cached ISOs into version control.
This Packer configuration retrieves the Ubuntu 22.04.3 ISO from the vSphere datastore.
To use this Packer file, download the Ubuntu 22.04.3 ISO here, then upload it to a vSphere datastore.
Next, update the iso_path
in vsphere-iso_basic_ubuntu.pkr.hcl
to point to the datastore containing the Ubuntu ISO, by replacing vsanDatastore
with your datastore name.
Next, initialize the Packer configuration.
Finally, build the Ubuntu template to your vSphere cluster.
Tip
This Packer configuration should work for most vSphere clusters. Refer to the Packer Builder for VMware vSphere documentation to customize this template to your exact vSphere environment.
Verify that Packer successfully created the template and loaded it into to your vSphere cluster.
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.
- The output block will display the
vsphere_vitrual_machine.learn
's IP address.
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
and modify the values to match your vSphere cluster.
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 viewing the vSphere Client.
View the recently provisioned VM's IP address.
Notice that it returns an empty list. This is because the VM did not have an IP address immediately after Terraform provisioned it.
Terraform can pull in the current VM state using terraform refresh
. Refresh your configuration's state.
Terraform successfully retrieved the VM's current state.
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 modified the VM successfully by viewing the vSphere Client.
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 viewing the vSphere Client.
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