Monthly Archives: November 2019

Terraform Azure Provider – Deploy a Virtual Machine and Join Domain

I’ve spent quite a lot of time working with Terraform over the past few months both at work and at home, so far I’ve been concentrating on the vSphere provider, I thought it was time to take a look at the Azure provider.  The beauty of Terraform is that it’s vendor agnostic, so once you understand how the logic works, in theory it should be a relatively painless transition to working with a different provider.   I’ve been using Azure for the past year or so, mostly for personal projects and for experimentation, during my study for the Azure Certified Administrator I worked with Arm templates and found them to be quite cumbersome,  they require an awful lot of code just to deploy a simple virtual machine.  Take a look here for an example of an Arm template to deploy a VM, then compare it to my completed test project on GitHub.

My project goes a step further also joins the machine to the domain, it will deploy the following…

  • Create Resource Group
  • Create VNET
  • Create Subnet
  • Create Public IP Address
  • Create Network Security Group and open RDP firewall port
  • Create NIC
  • Create Virtual Machine
  • Join virtual machine to custom domain (optional)


If you want to quickly deploy some infrastructure to Azure.

  • Create a clone of my repository to your local workstation.
  • Run ‘terraform init’ to download the Azure provider plugin
  • Edit with your subscription ID

  • run ‘terraform plan’
  • run ‘terraform apply’ once you are happy with the plan

This was my first attempt at deploying infrastructure into Azure using Terraform, I had everything up and running in a couple of hours, I accept it’s not the most complex deployment, but it was a useful starting point and one from which I intend to expand upon in the very near future.

My next challenge is to deploy some infrastructure in AWS using Terraform, hopefully a blog post will follow soon.

I’m still working away on #100DaysofCode(Python), I do need to check how many days I’ve got left as I’ve lost track! I must be fast approaching 100 days.  I’ll put up another post once I’ve caught up with things, thanks for reading!


Terraform vSphere Provider – Deploy multiple virtual machines of varied specifications – Part 2

Reminder of Objectives

  • Deploy multiple virtual machines of varying RAM, CPU and Disk Sizes.
  • Create 2 Virtual Machine Folders.
  • Create a Virtual Distributed Switch.
  • Attach the new virtual machines to the new VDS.
  • Place the new Virtual Machines in the correct folder


In Part 1  I went through some folder structure and suggestions for organising the terraform files, now it’s time to start populating the files, i.e. terraform.tfvars, and   I found when I started working on this mini-project that not many people have blogged about using the Terraform vSphere provider, the documentation provided by HashiCorp is quite detailed and they also provide a number of use case examples so it was a useful jumping off point, having said that… there was whole lot of trial and error involved to get the solution working.  I’ve uploaded the finalised project to GitHub which can be found below.

Final GitHub Project

The main challenge I had was creating multiple virtual machines of differing sizes,  the solution was to define a ‘list’ variable for each virtual machine type, in the example on GitHub I’ve split them up in the ‘Web’ and ‘App’ virtual machine types, below is an example from the files, here I declare the variable for machine types of ‘Web’ and ‘App’

Once the variables have been declared we then need to populate terraform.tfvars with the virtual machines specifications such as name, cpu, ram etc… code blocks

There are 3 code block types in the file, ‘provider’, ‘data’, and ‘resource’

  • provider block – This denotes the type of terraform provider to be used for the project, in this case it’s vSphere.
  • data blocks – represent infrastructure that you want to deploy on to, for example the VCSA, Datacenter, ESXi hosts etc…..
  • resource blocks – represent new infrastructure, so what you intend to deploy e.g. VDS, virtual machines, folders etc..

The next step is to populate the file,  we will add the virtual machine as a ‘resource’ block, as you can see Terraform will perform a ‘count’ operation on the list we populated in the terrafrom.tfvars file, it’s basically a ‘for’ loop, so for each indexed item in the list it will apply the values for CPU,RAM,  etc…. to the indexed item, you can have as many virtual machines here as you want, for the purpose of demonstration I’ve included 2 virtual machines per virtual machine type.  There’s lots of other stuff going on here such adding a static Mac-Address, adding the machine to the new port group on the VDS and adding multiple disks,  I’ve made use of ‘lists’ in a number of places, if you look at the completed code on GitHub most of it should be quite self explanatory.  If anything is unclear feel free to post a message and I’ll get back to you asap.