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

[Azure] Deploy resources with Terraform

  • July 30, 2019
  • 14 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:

https://www.terraform.io/downloads.html

You have some code examples here: https://registry.terraform.io/search?q=azure

The documentation for the AzureRM Terraform provider is available here: https://www.terraform.io/docs/providers/azurerm/index.html

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

  • provider.tf that will contain information to connect to our Azure Subscription
  • maint.tf that will contain resources that we want to create, and call of modules
  • variables.tf that will contain values of resources that we want to create
  • modules folder
    • a sub folder 1-network
      • a file 1-create_base_network.tf
      • a file tf
    • a sub folder 2-windows_vm
      • a file 1-virtual-machine.tf
      • 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 provider.tf 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 main.tf 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 variables.tf 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 1-create_base_network.tf and variables.tf 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 values.tf file. Also note the use of output to use the values of this file, directly in my main.tf file with the module value:

Here are the 1-virtual-machine.tf and variables.tf 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

Network-CloudyJorney

The advantage of Terraform is that you can only change what you need. For example, if I add in my 1-virtual-machine.tf 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: https://github.com/Flodu31/Terraform/tree/master/Deploy_New_Environment

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.

Hey! Found Florent’s article helpful? Looking to deploy a new, easy-to-manage, and cost-effective hyperconverged infrastructure?
Alex Bykovskyi
Alex Bykovskyi StarWind Virtual HCI Appliance Product Manager
Well, we can help you with this one! Building a new hyperconverged environment is a breeze with StarWind Virtual HCI Appliance (VHCA). It’s a complete hyperconverged infrastructure solution that combines hypervisor (vSphere, Hyper-V, Proxmox, or our custom version of KVM), software-defined storage (StarWind VSAN), and streamlined management tools. Interested in diving deeper into VHCA’s capabilities and features? Book your StarWind Virtual HCI Appliance demo today!