Lately, I took the decision to do not have anymore a physical lab, even if it was already hosted and managed at a service provider, but to completely nest it inside a vCloud Director tenancy. But while I was planning the rebuild operation, I also decided it was time to make its creation process as automated as possible, and while doing so, I learned a bit about how to use Terraform.
Some basics
No, I’m not going to write yet another blog post about Hashicorp Terraform and how to use it. You can look for this at the official userguide, or search around for many articles. Here, I’m going to describe the way “I” used it, from the point of view of a total newbie.
Terraform is a powerful tool to make your own “Infrastructure as code”: that is, a language where you can describe your infrastructure in any detail, and then build it, modify it, or even destroy it, without having to do anything manually, but by simply executing the tool and using the configuration as the input.
Initial configuration for vCloud Director
The first thing we have to do is to download the software, for Windows in my case. It comes in a single executable file that is around 70 MB, and for my ease of use I’ve placed it into a dedicated folder, that is c:\terraform.
Once we have the software, we need to initialize the environment by telling Terraform which platform we want to use. Terraform has plugins (or “providers” in Terraform terms) for many environments, like AWS, Google Cloud, Azure. In our case, we need the official provider for vCloud Director. but also for providers, we don’t have to do anything manually, we just configure the system to consume vCloud Director, and during the first initialization, Terraform will download automatically the needed software.
So, let’s talk about the configuration. Terraform uses files where the code is written in a specific language, and they have to be saved with a .tf extension. All files in a folder are parsed and applied, so we can split the description of the infrastructure into multiple files. The first files are variable.tf (where i list all the main variables I use):
variable “vcd_user” {
description = “vCloud user”
}
variable “vcd_pass” {
description = “vCloud pass”
}
variable “vcd_org” {
description = “vCloud org”
}
variable “vcd_vdc” {
description = “vCloud VDC”
}
variable “vcd_url” {
description = “vCloud url”
}
And terraform.tfvars where I store all the parameters of the main variables:
vcd_user = “user”
vcd_pass = “password”
vcd_org = “org”
vcd_vdc = “vdc”
vcd_url = “https://vcloud.provider.com/api”
(I’ve replaced the real login information with fake data)
With these files in the folder, we can open a command prompt and already initialize Terraform, by using the command terraform init:
As you can see, Terraform downloaded the plugin for the vCloud Director provider. You can also initialize the environment using terraform init -upgrade so that Terraform will always check for new versions of the plugin and download them:
Let’s create some networks
Exactly like during a manual configuration, the first operation inside vCloud Director will probably be to create the needed networks. I describe this network in my configuration, like this:
provider “vcd” {
user = “${var.vcd_user}“
password = “${var.vcd_pass}“
org = “${var.vcd_org}“
vdc = “${var.vcd_vdc}“
url = “${var.vcd_url}“
}
resource “vcd_network_routed” “net1” {
name = “vcc-management”
edge_gateway = “VeeamLab_Edge”
gateway = “10.10.51.254”
netmask = “255.255.255.0”
dns1 = “10.10.51.21”
dns2 = “10.10.51.22”
dns_suffix = “cloudconnect.local”
dhcp_pool {
start_address = “10.10.51.101”
end_address = “10.10.51.120”
}
}
It’s finally time to execute the code. This is done by running terraform apply (I’m using Terraform 0.12, versions before 0.11 use the plan command):
I answer yes, and I let Terraform complete the task.
And we have our first vCloud Organization Network, routed via a new NSX Edge firewall:
And all of this, without having to do a single click in the vCloud director interface!
Next time, I will start to build my virtual machines.