Part 2: Terraforming my vSphere homelab

Aleksandra Todorovska
4 min readJan 7, 2024

Actual terraforming

Introduction

This is the 2nd post of a series where the first part is mainly personal takeaways on a learning journey. This post tackles the technical “Getting started”. It’s a simple walkthrough of setting up locally in Windows for Terraform, a remote repository with a free account on Azure Devops and baby steps into terraforming code.

Local Windows Terraform setup

The setup is quite simple and involves:

  1. The download of the appropriate binary

2. The ”installation” of the Terraform binary by adding it to Window’s environment variables so that you can use it globally, in whichever directory.

3. Then in your code editor of choice, just write terraform to verify it’s been setup.

If by any chance, the time has come to upgrade your terraform binary, this Stackerflow post helps explain how to do that if you do not use package managers like Chocolatey.

Remote repository

Starting point should be to install Git, if that hasn’t already been done. If not, it can be done by following the instructions here.

Then, to set up a free repository (initially you create an organization and project) on Azure Repos, a Microsoft account is needed, so for that you can just create an @outlook.com email or sign in with GitHub.

After you’ve cloned that repository locally on your machine, it will be connected so that you can “upload” all the changes done in that version with git commands.

Learning how to “git” is a separate topic on its own. But one can get by with only the basics, for which this cheat sheet always comes in handy. Or if you’re using Visual Studio Code, a lot of those can also be done with the GUI it offers.

Actual Terraforming: Creating folders

When starting out, I really didn’t put too much effort into the best practices. I just wanted to see Terraform in action.

For that purpose, what I started out with was creating folders in the vCenter from my homelab. Something to keep in mind when using Terraform in a vSphere environment, is that you cannot get by only with Esxi and with unlicensed version.

As emphasized in Hashcorps docs on the vsphere provider

There are several ways to get at least eval licenses:

Now that you have your vSphere enviroment all set up and “licensed”, let’s get to the actual terraform code.

My only division of terraform files were the:

  • main.tf, which has data source blocks but mostly resource blocks of what I wanted to create

# Describe to Terraform an existing vSphere datacenter
data "vsphere_datacenter" "dc" {
name = var.dc_name
}

data "vsphere_datacenter" "dcR" {
name = var.dc_nameR
}

#########
#Managing folders
#########
#1st level of folders, Test

resource "vsphere_folder" "Test-Rasp" {
path = "Test-Rasp"
type = "vm"
datacenter_id = "${data.vsphere_datacenter.dcR.id}"

}

resource "vsphere_folder" "Test" {
path = "Test"
type = "vm"
datacenter_id = "${data.vsphere_datacenter.dc.id}"

}

#1st level of folders, Prod
resource "vsphere_folder" "Prod-Rasp" {
path = "Prod-Rasp"
type = "vm"
datacenter_id = "${data.vsphere_datacenter.dcR.id}"

}

resource "vsphere_folder" "Prod" {
path = "Prod"
type = "vm"
datacenter_id = "${data.vsphere_datacenter.dc.id}"


}
  • providers.tf, this is something standard and you just need to adjust the version usually to the latest one at moment you’re reading this
terraform {
required_providers {
vsphere = {
source = "hashicorp/vsphere"
version = "2.6.1"
}
}
}

# Connect to a given vCenter server
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_server
allow_unverified_ssl = true
}
  • terraform.tfvars.tf
variable "vsphere_user" {
default = "username@domain"
type = string
}

variable "vsphere_password" {
default = "......."
type = string
sensitive = true
}

variable "vsphere_server" {
default = "IP address"
type = string
sensitive = true
}

variable "dc_name" {
default = "actual dc name"
type = string
}

variable "dc_nameR" {
default = "actual dc name"
type = string
}

The first time and mostly probably only time, unless changes are done to the directory where all your importan terraform files are place, the first command that should be run is:

terraform init

Then once you’re ready to create your infra objects, but want to verify the code is valid, you can run:

terraform validate

After that, to basically do a a dry run of what your changes will be and optionally save them to a named plan, run:

terraform plan -out "Name of Plan"

If that doesn’t return any errors, just announces that your desired changes will be done, run:

terraform apply "Name of Plan"
Terraform in Action: Creating folders

Conclusion

Now you’ve seen Terraform in action with some basic folder creation. The following post coming soon will cover on how to this in a better, DRY and in accordance with best practices way.

--

--