StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

[Azure] Deploy resources with Terraform

  • July 30, 2019
  • 8 min read
Cloud and Virtualization Architect. Florent is specializing in public, hybrid, and private cloud technologies. He is a Microsoft MVP in Cloud and Datacenter Management and an MCSE in Private Cloud.
Cloud and Virtualization Architect. Florent is specializing in public, hybrid, and private cloud technologies. He is a Microsoft MVP in Cloud and Datacenter Management and an MCSE in Private Cloud.

Currently, we are speaking a lot about IaC, Infrastructure as Code. That gives you the opportunity, to deploy an infrastructure, from 0, with code, without being a great developer (I assure you, me and the code, it’s not a great love story 🙂). You currently have the following tools to do IaC:

  • PowerShell DSC
  • Ansible
  • Puppet
  • Chef
  • Azure Resource Manager
  • Etc.
  • And of course, Terraform

In the following article, I’ll show you how to deploy, from scratch, via terraform (who is an open-source software, provided by the Hashicorp’s company), a test environment on Azure. It will include a VNet, and a Windows Server VM with a public IP.

To start, you need to download and install Terraform:

You have some code examples here:

The documentation for the AzureRM Terraform provider is available here:

Now that Terraform is installed, we will create some files:

  • that will contain information to connect to our Azure Subscription
  • that will contain resources that we want to create, and call of modules
  • that will contain values of resources that we want to create
  • modules folder
    • a sub folder 1-network
      • a file
      • a file tf
    • a sub folder 2-windows_vm
      • a file
      • a file tf

A modules folder

I will use modules. The advantage of using modules is the fact that you can reuse modules in other projects later, without redoing the code. Here is my file that contains the information to connect to the environment (if you want to specify the version of the Azure RM provider to use, insert version = “= 1.22.0”, otherwise do not put anything to use the latest version):

Following is my that will create a network resource group, create the virtual network by calling the network module and create the Windows VM by calling the windows module:

The file will contain values for resources that we will deploy. Here, my password is in clear text, but it is possible to get the password from an Azure Keyvault for example (see next articles):

My file and are as follows. They will deploy a VNet with a LAN subnet. Note that it is possible to add default values in variables, by adding for each variable default = “value”. Here I do not use it, to force the values in the main file. Also note the use of output to use the values of this file, directly in my file with the module value:

Here are the and files that will deploy the Windows VM using the previously created VNet:

Save everything. It is now time to execute the commands for the deployment of our environment. Go to the main folder and do a terraform init to initialize the project:

Terraform init

Execute after terraform plan to see what it will be do in the subscription. If you have errors, it’ll be here. Here, it will add 5 resources:

  • A resource group
  • A VNet
  • A subnet
  • A NIC
  • A Windows VM

Terraform plan

To start the deployment, do terraform apply and confirm the deployment by clicking on yes:

Terraform apply

After few minutes, resources have been deployed:

Resources have been deployed


The advantage of Terraform is that you can only change what you need. For example, if I add in my file:

And that I do terraform plan, it will compare what has been done in the past (this is stored in a terraform.tfstate file) with what we have to do now. In our case, he will add a resource. It’s the same with the modification / deletion:

Terraform plan

If you want to delete that you deployed, execute the command terraform destroy:

Terraform destroy

After few minutes, everything has been deleted:

Results of Terraform destroy

All of this code is available here:

Here’s a simple example of deploying with Terraform. Of course, it is possible to go much further. In future articles, I’ll talk about recovering sensitive values in a keyvault and also running commands directly into a VM after the deployment.

Found Florent’s article helpful? Looking for a reliable, high-performance, and cost-effective shared storage solution for your production cluster?
Dmytro Malynka
Dmytro Malynka StarWind Virtual SAN Product Manager
We’ve got you covered! StarWind Virtual SAN (VSAN) is specifically designed to provide highly-available shared storage for Hyper-V, vSphere, and KVM clusters. With StarWind VSAN, simplicity is key: utilize the local disks of your hypervisor hosts and create shared HA storage for your VMs. Interested in learning more? Book a short StarWind VSAN demo now and see it in action!