Deploying vSphere VM with Terraform

This article is outdated!
Unfortunately, this article is outdated, so please be careful when you will implement this solution!

Do you know when infrastructure deploying was revolutionized? It was the day when the world saw the Terraform for the first time. Yes, Infrastructure as a Code is a great idea which can simplify our infrastructure provisioning. We can see the full power of the Terraform just with an AWS, but it can be also used with another Cloud Providers. In the Terraform documentation, you can find Google Cloud, Digital Ocean, Microsoft Azure, but also private and on-premise providers like Openstack or VMware vSphere. I work with VMware vSphere and VMware vCloud Director, so Terraform is designed for me too!

What is Terraform?

If you have never heard about Terraform, I need to write a few words about it. It’s a tool for deploying, versioning and controlling the infrastructure. Terraform has a client-side architecture. It means that it do not need any agent installed anywhere. The entire communication is conducted by the providers API requests. One of the most important features of this tool is a declarative code. You can write a set of instructions for Terraform, and it will do everything for you! All you need to do is declare the end state of your infrastructure. It’s pretty convenient, isn’t it?

Maybe you can find it interesting!
Deploying multiple vSphere VMs with Terraform
But what about deploying vSphere VM with Terraform?

Now, vSphere Provider is not as expanded as AWS Provider, but has enough functionality for most cases. You can use it for single VM deploying as well as for multiple VMs. If you want to have complete builds, you may meld Terraform and Ansible together. Please notice that Terraform has ability to make OS customization, so once the build is completed, you have a full operational Virtual Machine! Deploying vSphere VM with Terraform is easy – there are only few variables and you can split config into the three files.

  • provider.tf
  • instance.tf
  • vars.tf

Of course I do not want to explain how the entire Terraform works. You can check First Steps in the official documentation or another blogs. I also recommend Learn DevOps: Infrastructure Automation With Terraform on the Udemy. The whole mentioned pages are focusing on basics and explain it better than I. I just want to show you how to deploy vSphere VM with Terraform.

Finally, how to use Terraform with vSphere?

I use Terraform with a few cases, but but honestly – just two basic build templates are enough for me. Today I am presenting you the first example – deploying a single VM. You can clone from GitHub my Git repository containing something I called template.

vSphere VM with Terraform repository

As you can see, there are only three files (excluding README). You do not need to change provider.tf (unless you want to disable “allow_unverified_ssl” variable – it is useful with self-signed certificates). If you list the vars.tf file, you see that I defined every important variable with default values. For example:

Every variable is described, every map has a short instruction how mapping works. Of course values in the repository are fake – you need to change values on your own. Yup, every single value. But don’t worry – it’s just one-time operation. When you suplement the vars.tf file, you can use it many times, changing only that variables, which should be modified.

Example of vars.tf

Let’s assume that you have infrastructure like this:

Example infrastructure

In that case your vars.tf file will look like this:

Yes, I know it’s pretty long but, despite appearances, it’s obvious.

Another changes you should make are IP addresses variables. In my template you can use multiple domains within one vCenter Server. You just need to specify DNS servers for each domain and be sure that you use the right “vmdomain” variable. I assumed that VMs would be in the same subnet as domain controllers, but of course it’s only the simplification. You can specify another subnets with another subnet masks.

How to specify VM – instance.tf

Information you see above is only a definition of your infrastructure. It’s important, but you cannot deploy a vSphere VM with Terraform if you do not code an instance definition. Terraform uses a resource block for it. It’s recommended to use a separate file (for example instance.tf) for code clarity.

In my template, instance.tf file contains only one block with two subblocks. Terraform allows you to create additional disks, network cards, but this case describes the simplest example. Only one base disk and one network adapter.

As you can see almost everything is defined as a variable. You don’t need to change every variable when you want to deploy a VM in – for example – another Datastore. You only need to specify few vars and the variable maps will do work for you. And here the entire magic begins!

Use a .tfvars file to protect your secrets

It’s not recommend to store your secrets in the version control systems. This rule concerns a vSphere Provider as well as another Providers. There is a special file with a .tfvar extension in which you can put your credentials and also overwrite default variables from the vars.tf file. By design, this file is used by Terraform to populate variables in the build, so you can use it for both options.

You should not add a .tfvars file to your repository. Please take a look at my .gitignore file in the terraform-vsphere-single-vm repository. Git will ignore these files and that’s the point, because I put credentials inside them. Apart from the credentials, you should put every variable you want to overwrite a default value. If you want to deploy vSphere VM with Terraform using my template, it should be at least:

  • vmdomain – domain name for OS customization; some maps are based on that value
  • vmrp – Resource Pool in which you want to deploy your VM

Rest of variables you can leave with default values. I highly recommend you to set your defaults with the resources you can use for testing purposes. Then if you forget to change defaults, nothing bad will happen.

Example of the .tfvars file:

When you make required changes, you can build your vSphere VM with Terraform, using terraform plan and terraform apply command (look here for more details). That’s all!

Technical Notes

During the constructing your infrastructure code, you should pay attention to the following things:

  • ensure the resource_pool variable in the instance.tf file has a full path, for example CLUSTER_NAME/Resources/RP_NAME
  • ensure the template variable in the instance.tf file has a full path to the VM template (based on folders), for example Templates/Linux_Templates/TEMPLATE_NAME
  • ensure the name variable in the instance.tf file is compatible with the following requirements (more details here):
    • variable doesn’t contain any underscores
    • variable length is less than 15 characters
  • this template works only with Linux machines
Summary

With this template you can easily create a vSphere VM with Terraform. It has only basic options, but Terraform vSphere Provider contains many more possibilities. You can deploy multiple VM at the same time, create additional disks and network adapters, customize Linux and Windows OS, run scripts after successfull deployment, upload .vmdk files etc. You can check all of the possibilities in the Terraform Documentation, especially vSphere Provider section.

I will certainly create another post (and GitHub repository) with template that allow deploying multiple vSphere VM with Terraform, but you can try to play with this tool in your own labs. The greatest fun begins with using Terraform, Ansible and Jenkins together, but I will write about it next time. 🙂

Bibliography:

2 Replies to “Deploying vSphere VM with Terraform”

  1. Nice intro but the most important part would be how to go on after you deployed the basic infrastructure. There has to be some adjustment on the DHCP/DNS-Server to make it work with Ansible (or another configuration management tool). I was missing that area …

    1. Hi! Thanks a lot for your feedback. To be honest, it’s quite outdated post… I have not been working with VMware for about a year, so it’s hard for me to update and add some more topics. But anyway thank you very much, because I recalled this article. And I should mark it (and similar articles) as outdated. 🙂

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.