Category Archives: Terraform

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.


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

I recently had the opportunity to work with Terraform which is HashiCorp’s Infrastructure as Code (IaC) offering, it’s an interesting tool as it’s vendor agnostic and can be used with AWS, VMware, Azure and many more software vendors.  Organisations seem to be heading towards a mix of multi-cloud and on-premise Infrastructure which means more tools and more skills to learn for the people supporting it, Terraform seems like it could be a really good choice to help cut down on complexity and staff training/re-training.

The organisation I’m currently working for tasked me with developing a proof of concept so they can determine if the tool is a good fit for their business, they are primarily a VMware house with an increasing footprint in Microsoft Azure.  Terraform has the concept of  ‘Providers’, which basically contain the underlying code that performs all the heavy lifting and interaction with the vendor’s software, for example for VMware there is a vSphere provider, for Amazon Web Services there’s the AWS provider, for the purpose of this post I will be using the vSphere provider.


  • 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
  • Standardise the code on GitHub so it can be cloned and used on cross region.

Getting Started:

I’m going to assume that you already understand how get Terraform up and running on your workstation.  Once you’ve created a  folder and downloaded the relevant Terraform binaries we can start organising the folder structure.  I want to make sure that other admins can clone the git repo and modify the variables that are relevant to the site they are deploying, I would like to keep the editing of files to a minimum for other admins.

I prefer the structure below as it means that when somebody else  wants to re-use your code they only need to edit the terraform.tfvars file.

  • – This is where you define the provider and describe what you intend to deploy.
  • – For every variable in there needs to be a corresponding variable in
  • terraform.tfvars – This is the only file that needs to be edited once your code has been finalised.



I’ve decided to include a few steps on GitHub set up.

You can set up and account for free on GitHub, once you’re signed in create a new repo, to clone the repo you will need to install git tools to run the git commands, I’m on a Windows machine so I’ve used ‘Git for Windows’.

  • Clone the repo to your workstation

  • Initilaise the repo

Now that the repo is initialised you can create a new branch and start pushing changes to the main repo, below is a quick summary of commands to create a new branch.

  • git add .
  • git commit -m “Initial Commit”
  • git branch Initial_Branch
  • git push origin Initial_Branch

In part 2 of this series we’ll start editing the files…..