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

VSAN from StarWind eliminates any need for physical shared storage just by mirroring internal flash and storage resources between hypervisor servers. Furthermore, the solution can be run on the off-the-shelf hardware. Such design allows VSAN from StarWind to not only achieve high performance and efficient hardware utilization but also reduce operational and capital expenses.

Learn more about ➡ VSAN from StarWind

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.

Views All Time
3
Views Today
12
Appreciate how useful this article was to you?
No Ratings Yet
Loading...
Back to blog
The following two tabs change content below.
Florent Appointaire
Florent Appointaire is Microsoft Engineer with 5 years of experience, specialized in Cloud Technologies (Public/Hybrid/Private). He is a freelance consultant in Belgium from the beginning of 2017. He is MVP Cloud and Datacentre Management. He is MCSE Private Cloud and Hyper-V certified. His favorite products are SCVMM, SCOM, Windows Azure pack/Azure Stack and Microsoft Azure.