
michelangelus - Fotolia
Use Terraform and vSphere to declare and manage VMs
Terraform and VMware form an ideal combination for IT administrators seeking efficient ways to manage vSphere VMs and resource pools. Model your management on these code examples.
A mastery of Terraform and vSphere enables IT administrators to establish resource pools and declare VMs from scratch...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
or from a template with code.
Terraform is an infrastructure as code tool from HashiCorp that abstracts infrastructure configuration. The previous Terraform and VMware article in this two-part series explained how to use code to set up a vSphere provider, define the new data center, and create a host cluster and data store.
Once you get the infrastructure set up, you can build on these core components to create resource pools and VMs.
Build Terraform and vSphere management on resource pools
Resource pools enable you to carve out the resources of a host or cluster and fence VMs for CPU and memory reservations, shares and limits.
First, add a new resource -- vsphere_resource_pool -- to create a resource pool as part of your Terraform and vSphere deployment.
The following example creates a resource pool with the settings below:

This example declares that the resource pool named terraform-example-resource-pool will set share levels for CPU and memory to high.
Organize resources with vSphere folders
To keep your vSphere environment clean, use vSphere folders to organize your resources. Use code to declare folders for VMs, hosts and networks as part of your Terraform and vSphere deployment.
- name: terraform-example-vms type: vm
- name: terraform-example-vsphere-hosts type: host
- name: terraform-example-vsphere-networks type: network

Declare vSphere VMs
There are a few different methods to declare VMs in your Terraform and vSphere deployment. These examples involve creating 10 VM instances with the parameters below:
- os: Ubuntu
- memory: 512mb
- vcpu: 1
- os_disk: 36gb
- resource_pool: terraform-example-resource-pool
- folder: terraform_example_vms
Creating VMs from scratch is useful when you're using an external provisioning mechanism, such as a Preboot Execution Environment or the Trivial File Transfer Protocol. For our purposes, assume that external provisioning is in place and that the goal is to spin up all 10 VM instances with the above requirements.
This example uses a simple method to declare the resource vsphere_virtual_machine rather than creating 10 distinct resources. The counting loop in the code appends to the VM and acts as the basis for the generated VM names.
Declare vSphere VMs
There are a few different methods to declare VMs in your Terraform and vSphere deployment. These examples involve creating 10 VM instances with the parameters below:
- os: Ubuntu
- memory: 512mb
- vcpu: 1
- os_disk: 36gb
- resource_pool: terraform-example-resource-pool
- folder: terraform_example_vms
Creating VMs from scratch is useful when you're using an external provisioning mechanism, such as a Preboot Execution Environment or the Trivial File Transfer Protocol. For our purposes, assume that external provisioning is in place and that the goal is to spin up all 10 VM instances with the above requirements.

This example uses a simple method to declare the resource vsphere_virtual_machine rather than creating 10 distinct resources. The counting loop in the code appends to the VM and acts as the basis for the generated VM names.
Create VMs from templates
Templates make provisioning much faster because you're cloning an existing template. The following example uses a template called example_ubuntu_template:

This example creates a data type that references a template in your vSphere environment. Defining this enables you to define your disk section so it uses the details from the template. This example also defines the clone section to include the template_uuid, which captures the universal unique identifier of the source template.
The Terraform and VMware vSphere combination adds effective and efficient management possibilities to vSphere deployments. By mastering Terraform, you can create a functional vSphere deployment that you can manage entirely with code.