Create Your First vSphere Terraform Configuration

Reading Time: 3 minutes

To get started with HashiCorp Terraform, you can download the simple executable file from HashiCorp here. To understand the basics of Infrastructure as Code and Terraform, have a read of the post available here.

Terraform configuration files are text files with a .tf file extension. To get started, create a new folder for this configuration, along with a file called The first part to creating a Terraform configuration file is to define the provider you will interact with. In this example, it will create a virtual machine using the vSphere provider. The vSphere provider has some required fields like the user, password and server name. Create the provider block referring to “vsphere” and set the required fields with valid values for your environment.

Following the provider, define any “data” sources needed, which are used to retrieve existing objects. In this example of creating a vSphere virtual machine, reference the existing data center, cluster, datastore, VM network and template objects. This is defined in the code below. You can see that the “name” values are used to locate each of the objects. These will be referenced later to retrieve the ID values, as you can also see in the “vsphere_resource_pool” data source.

Once you have all of the required data sources, define the “resource” that is to be created. In this case, it will be the “vsphere_virtual_machine” type, where the configuration of the virtual machine is defined. Start by defining the VM name, along with how many CPUs and the amount of memory to be assigned (in MB). There are a few required fields such as the datastore_ID and resource_pool_ID, which are referred to using the “data” sources created earlier. Then set the guest_ID and scsi_type to reference the guest_id and scsi_type of the template VM by referencing the “data” source.

The network_id in the network_interface block is set to match the “data” source of the vSphere network.

The disk block is then defined where the VMDK label is defined, along with the size of the disk to be deployed (in GB).

The clone block is next, where the template VM is defined, referring to the “data” source of the template. The customization options are then set where the host_name, domain and IP configuration is set.

The final block in the “resource” is the remote-exec provisioner. A remote-exec provisioner is used to run a command or script on the provisioned resource. The script to be called is specified along with how to connect to the VM. In this case, a connection is made using the root credentials to the default IP address of the provisioned VM. The contents of the example script are also shown below.

The final file will look like this. It will contain the provider, data and resource elements.

Note: Do not store your configuration in a version control system such as GitHub or GitLab as this example stores your credentials in the file. Instead, these would typically be stored in variables, where the variables are excluded from your git commits.

Once your file is complete, run the “terraform init” command in the folder which contains your file. This will automatically download any providers that are referenced in your Terraform configuration file. Next, run the “terraform plan” command which will show you exactly what resources are to be created. In this example, it will be one vsphere_virtual_machine. If the plan looks correct, run the command “terraform apply –auto-approve” to provision your virtual machine. Congratulations! You should now have your new VM deployed in your vSphere environment.


One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.