A whale ago, I started using Packer for creating Windows Templates for using in my home lab.
I blogged about it here: Windows Server 2025 “Preview” deployment with Packer
So now I want to deploy a template with Terraform, which was created earlier by Packer.
First, we need to ensure we have the Terraform downloaded and the vSphere provider Initialized.
- Install Terraform: You can download it from the official Terraform website and follow the installation instructions for your operating system.
I made my life a little easier to create a PowerShell script that downloads the latest version: Download Terraform.ps1
- Initialize Your Terraform Configuration: Create a directory for your Terraform configuration files. Inside this directory, create a `main.tf` file (or any `.tf` file) where you will define your provider and other configurations.
Define the vSphere Provider in Your main.tf: Add the following block to your Terraform configuration file to specify the use of the vSphere provider:
terraform {
required_providers {
vsphere = {
source = “HashiCorp/vsphere”
version = “> 2.11”
}
}
}
provider “vsphere” {
user = vcenter_username
password = vcenter_password
vsphere_server = vcenter_server
# If you have a self-signed cert
allow_unverified_ssl = true
}
Replace ”vcenter_username”, ” vcenter_password” , ”vcenter_server” with your actual vSphere credentials and server address.
- Basic Terraform Commands
Terraform.ps1
# Go to the Terraform download folder
$terraformfolder = ‘d:\automation\terraform\’
Set-Location $terraformfolder
# Download Terraform plugins
.\terraform.exe init
# Test Run
.\terraform.exe plan
# Run Terraform
.\terraform.exe apply
# Clean Up what you created
.\terraform.exe Destroy
Initialize the Terraform Working Directory: Run the following command in your terminal from the directory where your Terraform configuration file is located:
terraform init

This command will download the vSphere provider and initialize your Terraform working directory. It sets up and downloads the necessary provider plugins for Terraform to interact with vSphere.
Verify the Installation: After running `terraform init`, you should see output indicating that the vSphere provider has been successfully installed. You can now proceed to create Terraform configurations to manage your vSphere resources.
Deploy a Template VM within vSphere
In this example, we are deploying a VM running Windows Server 2022.
Terraform Module Structure
- variables.tf: Define the variables for the module.
- main.tf: Contains the main configuration for the VM.
- outputs.tf: Define the outputs for the module.
Variables.tf
variable “vsphere_user” {
default = “<your_vcenter_username_here>”
description = “vSphere username to use to connect to the environment – Default: administrator@vsphere.local”
}
variable “vsphere_password” {
default = “<your_vcenter_password_here>”
description = “vSphere vCenter password”
}
variable “vsphere_server” {
default = “<your_vcenter_here>”
description = “vSphere vCenter server”
}
variable “datacenter” {
default = “<your_datacenter_here>”
description = “vSphere datacenter”
}
variable “cluster” {
default = “<your_cluster_here>”
description = “vSphere cluster”
}
variable “network” {
default = “<vcenter_network>”
description = “vSphere network”
}
variable “datastore” {
default = “<your_destination_datastore_>”
description = “vSphere datastore”
}
variable “template” {
default = “<your_template_name_here>”
description = “Template VM”
}
variable “customization_specifications” {
default = “<vcenter_customization_specifications>”
description = “Customization Spec”
}
When you run “.\terraform plan” you do a test run to check your coding


When you run “.\terraform apply” build your virtual machine


Based on: https://vminfrastructure.com/2025/03/11/deploying-a-vm-using-terraform/
It works but it need some tweaking and fixing.
Adding a TPM2.0 security device does not work right now
Server 2025 customization does not work
Like this:
Like Loading...
You must be logged in to post a comment.