Install and Configure Microsoft DNS on VMC / vSphere through Guest Customizations with Terraform

In this blog, I will walk you through how to use Terraform vSphere Provider to perform the following

What does Terraform Script Configure

  • Create Content Library
  • Upload Windows Template to Content Library
  • Deploy VM from Windows Template
  • Install & Configure Microsoft DNS Server through Guest Customizations with Terraform
    • DNS Primary Zone
    • DNS A Record
    • DNS Forwarder
    • Stop/Start DNS service

This is done using run_once_command_list where you can provide a list of commands to run at first user logon, after guest customization.



Following are the pre-requisites

  1. VMC / vSphere Endpoint Details are updated in terraform.tfvars
  2. Template Credentials are updated in
  3. This script assumes has DHCP enabled.
  4. This script assumes that you have network connectivity to vCenter/ESXi host where it needs to be deployed
  5. This script assumes that you have outbound connectivity from where you are running the terraform code
  6. This script assumes your windows template is on a web server reachable from where you are running the terraform code

I have validated with Windows 2012 Template which I had. It might require slight changes based on your template.


The script is published on my GitHub. It uses Terraform vSphere Provider

Clone my git repository locally and navigate to the folder Install-MicrosoftDNS

Update the terraform.tfvars with following

  • Environment details
  • Windows Template Location & Name

Execute following commands

terraform init

terraform plan









terraform apply -auto-approve










In my environment, it took 20-25 mins on VMC. It will depend on how quick your connection is from where you are executing the script

DNS Config

We run the following Powershell commands for DNS configuration as part of Guest Customizations with Terraform

“cmd.exe /C Powershell.exe -ExecutionPolicy Bypass Install-WindowsFeature -Name DNS -IncludeManagementTools”,
“cmd.exe /C Powershell.exe -ExecutionPolicy Bypass Add-DnsServerPrimaryZone -Name vmclab.local -ZoneFile vmclab.local.dns”,
“cmd.exe /C Powershell.exe -ExecutionPolicy Bypass Add-DnsServerResourceRecordA -Name mmdemo-host -ZoneName vmclab.local -AllowUpdateAny -IPv4Address”,
“cmd.exe /C Powershell.exe -ExecutionPolicy Bypass Add-DnsServerForwarder -IPAddress”,
“cmd.exe /C Powershell.exe -ExecutionPolicy Bypass net stop dns”,
“cmd.exe /C Powershell.exe -ExecutionPolicy Bypass net start dns”










You can reach out to me via Twitter if you need further details

Leave a Reply

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

%d bloggers like this: