Deploying multiple vSphere VMs with Terraform

Automation doesn’t mean only the ability to automate some process and repeating it over and over again. It means also the ability to automate every automated process – if it’s possible of course. So even if we can automatically deploy VM, why shouldn’t we try to deploy multiple machines? Everything is fine until we need to deploy three, four or more same virtual servers. It would be very annoying unless we automate also this process – process of deploying multiple vSphere VMs with Terraform.

If you haven’t read my previous article, you can do it here: Deploying vSphere VM with Terraform. I explained how to create a single VM. It can be used to deploy the same VM many times, but you have to do it manually. It means you need to iterate the following steps:

  • run terraform apply
  • wait for the completion of the deploy
  • change important elements (IP, hostname etc.)

If you want to deploy – for example – three virtual machines with the consecutive addresses and/or hostnames, probably you would prefer to do it with only one run. Of course, it is possible, and I will show you that.

Prepare configuration for creating multiple vSphere VMs with Terraform

Terraform configuration files don’t allow to use loops in the typical meaning of this word. Anyway, it doesn’t mean that you cannot iterate anything. Terraform allows using a meta-parameter called “count”. According to the documentation:

count (int) – The number of identical resources to create. This doesn’t apply to all resources.

That’s simple, right? It means exactly, that you can use this meta-parameter for creating multiple same resources. It is added in the resource section, like here:

In this scenario, we want to create three virtual machines with 2 vCPUs and 4 GB RAM. But do you see the problem? Yes, it’s the name of the virtual machine. If we leave this configuration, all of the VMs have the same name and hostname, what is unacceptable. Fortunately, it’s easy to change. We can use count.index for iterating the VMs names. For example:

This code will create three virtual machines named:

  • test-vm-1
  • test-vm-2
  • test-vm-3

Please notice, that we can perform a simple math like addition. It’s very useful when we want to manipulate the individual values. It can be used also with the IP address, like:

In this example, we will get virtual machines with the following addresses (assuming that count variable is equal 3):

  • 10.5.5.10
  • 10.5.5.11
  • 10.5.5.12

As you can see, counting starts at 0, and it’s not a surprise. It’s a normal iteration, and if you are familiar with some programming language you know that every loop starts iterate at 0 (in the most of the cases). You should remember that to avoid the wrong configuration. But, who cares? It’s possible to run terraform destroy, fix your HCL config files and run terraform one more time!

How to practice?

It’s very easy, so you can try making some exercises with your vSphere infrastructure. You will be notified by Terraform in case of any bad syntax. So the worst thing you can expect is the syntax error. Try, learn and automate your daily work!

If you want to see the more complex example, please visit my GitHub repository:

GitHub repository - multiple VMs

 

Leave a Reply

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