Last year I had the opportunity to spend quite a bit of time working with Terraform. For my surprise it was simpler than I expected. If you never used Terraform and have been dwelling with ARM templates, CLI and PowerShell, I highly recommend to give it a try.
Azure Terraform is not hard to learn but it helps to have a detailed explanation. This first part I will cover how to get started. Setting up your environment may be the simplest part if you have a good knowledge of the Azure resources that you are going to work with. In the end this is just a scripted approach to what you would do in the portal.
Once you complete this, you should have a dev environment to write terraform script for your whole solution. Part 2 will Cover how to move into Azure Devops.
Let’s first download all the Apps you are going to need and explain as we go:
- Download and Install Azure CLI (Just double click and run the install): https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest.
- Download terraform: https://www.terraform.io/downloads. This is not an install file. this is an executable file (terraform.exe). Save it in a folder like “C:\tools\Terraform” and add that location to your path system variable (you can use this command line: “set PATH=%PATH%;C:\tools\Terraform”. To check if terraform.exe is now in the “path” system variable, pop a command line and type: “terraform version”.
- Download Visual Studio Code: https://code.visualstudio.com/ (if you don’t have) and install the “Azure Terraform” add-on.

(The add-on page has some useful documentation: https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azureterraform)
After Installing the add-on, open the Visual Studio code terminal and type “Az login”


This will open a browser window for you to login into your azure. Once you enter your credentials and log in , you should see the terminal display details of your subscriptions. you can close the browser window after that.
If you have more than 1 subscription, you can make sure you are on the right one using this command:
az account set –subscription “Subscription Name”
For my subscription would be:
az account set –subscription “Azure subscription 1”
There is no Terraform project template in Visual Studio or a Terraform Project file. The simplest form is a single file called Main.tf. An ideal solution will have other files we will see later on the next post, Lets for now clone the code for this post (You can find it under : BrunoLucasBlog/TerraformStartSample )
Back to the terminal, create a directory for your code: mkdir c:/code/terraform

Clone and save to ‘c:/code/terraform”

For the future, there is a nice collection of scripts under the harshicop github (harshicop are the makers of Terraform). This one for example will create a resource group and a app service under it:https://github.com/hashicorp/terraform-provider-azurerm/blob/main/examples/app-service-environment-v3/main.tf.
To Run Terraform, type “terraform init” in the Visual Studio Code Terminal

If Everything is installed correct, this should say “Terraform has been successfully initialized” and you should notice a new file “terraform.lock.hcl”

type “Terraform Apply


Type “yes” to proceed and anything else to abort

Check the result in azure portal:

The above is just a super fast way to get all tools lined up and than move to the real world Terraform project structure and lifecycle. To create a Devops pipeline we will need to extend this code to accommodate different azure subscriptions and environments.
Leave a Reply