A simple Azure Terraform Walkthrough – Part 1

This series of posts is intended to assist in learning how to use Terraform with Azure – we will walk through a full deployment, covering off each step and all elements within the code. Within this series of posts I will walk through the following steps, breaking down the installation and Terraform code step-by-step to give an overview:

Part 1:
  1. Installation of Terraform, VSCode and other tooling using Chocolatey 
  2. Setup of the Terraform files and AzureRM provider
  3. Creation of the Variables including terraform.tfvars and variables.tf
  4. Setup of our deployment file for Azure Resources – in this case, it will be a single azuredeploy.tf file.
Part 2: (Click here for Part 2)
  1. Planning for our Deployment
  2. Deployment of the Terraform
  3. Testing
  4. Destroy all the things! (Cleaning up and destroying the Resources after they have been deployed).
Part 3: (Click here for Part 3)
  1. Tips, Tricks, and taking further steps

Note: to follow this series of posts through to deployment, you will need an Azure Subscription that you can deploy Resources into.


Step 1 – Installation

To get started, we need to install a few items – we will do this using the Chocolatey package manager, using the below PowerShell script. Run the below script as admin and it will install Terraform, VSCode, and the Azure CLI:

#Chocolatey setup and installation script for using Terraform
#Set Execution Policy to allow script to run
Set-ExecutionPolicy Bypass -Scope Process -Force 
#Chocolatey install
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
#Chocolatey apps
choco install vscode -y -no-desktopshortcuts
choco install terraform -y -no-desktopshortcuts
choco install azure-cli -y -no-desktopshortcuts

If you’d rather install manually, you can also do this following a guide here: https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/azure-get-started

We should now have the required software installed and can move onto setting up our Terraform files!

Step 2 – Setting up the Terraform files within VSCode, and the AzureRM Provider

To start setting up our environment, we will begin using VSCode and setup a folder to store our Terraform code. Create a folder on your machine and then open this within VSCode:

VS Code New Folder

Once this is done, your folder will be opened, and you can then select “New File”:

VS Code New File

Using the “New File” option, create the following files:

  • azuredeploy.tf
  • provider.tf
  • terraform.tfvars
  • variables.tf

VS Code Files

We’ve now setup our Terraform Files, and are ready to start populating these. Next, we will populate the provider.tf file.

Setting up the Provider

Providers are plugins that allow Terraform to interact with different platforms and services, and Providers add different resources and data sources that Terraform can work with. In this case, we will be using the AzureRM Provider, as this guide is aimed at deploying Terraform on Azure.

Using the AzureRM Provider is very simple (and supported by awesome documentation), we visit the Provider web page (from the HashiCorp Terraform Registry), and select “Use Provider”:

HashiCorp Registry

Once this has been selected, the Provider code block is shown, which gives us all of the information we need to use the Provider:

Azure RM Use Provider

This code should be copied and pasted into the provider.tf file. Note that we do need to add a features block as shown below. To make this simple, I’ve provided a full provider.tf below too. Be sure to save the file once you have pasted the code in!

VS Code Provider Block

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.6.0"
    }
  }
}

provider "azurerm" {
  features {
  }
}

That’s our Provider setup – and we can now move on to Step 3, which is the creation of our variables files (terraform.tfvars and variables.tf). Further information about providers can be read here: https://www.terraform.io/language/providers 

Step 3 – Creation of the Variables files

We will be using two files to define and describe our variables within Terraform:

  • Variables.tf – this file will be used to provide the type, and a description of each variable.
  • Terraform.tfvars – this file will be used to provide the variables that are specific to our deployment.

To keep things simple for this post, I’ve created some pre-populated code to get us started. Within each file, paste in the code below:

Variables.tf:
variable "azure-rg-1" {
  type        = string
  description = "resource group 1"
}
variable "loc1" {
  description = "The location for this Lab environment"
  type        = string
}
variable "region1-vnet1-name" {
  description = "VNET1 Name"
  type        = string
}
variable "region1-vnet1-address-space" {
  description = "VNET address space"
  type        = string
}

As you can see, within variables.tf, we are describing each variable, and providing a variable type along with the description. Within this file all of our variables are string variables, however, it is worth noting that there are other types. For more information on variables, see this link: https://www.terraform.io/language/values/variables

Terraform.tfvars:
azure-rg-1                  = "rg-demo-lab"
loc1                        = "uk south"
region1-vnet1-name          = "region1-vnet1-hub1"
region1-vnet1-address-space = "10.10.0.0/16"

Within Terraform.tfvars we are defining the specific values we will use for our deployment. These are the values that will be used when our code is deployed. Within most Terraform deployments, it is within the Terraform.tfvars and the deployment files (covered later in this post) that most of the tweaks/changes/work will take place.

Step 4 – Defining our Resources

For the final part of this blog post, we will create the required Terraform Code to deploy two simple Azure Resources – A Resource Group, and a VNET. I’ve kept the Resources to a minimum so that we can focus the tooling and process – and creating more complex deployments and code can come along later. I like to see this as a way to learn and understand the process with simple code, and then expand this to more complex code later – a walk before we run approach! 🙂

To make this simple – I have created the following for the azuredeploy.tf file:

# Resource Group
resource "azurerm_resource_group" "rg1" {
  name     = var.azure-rg-1
  location = var.loc1
}
# VNET
resource "azurerm_virtual_network" "region1-vnet1-hub1" {
  name                = var.region1-vnet1-name
  location            = var.loc1
  resource_group_name = azurerm_resource_group.rg1.name
  address_space       = [var.region1-vnet1-address-space]
}

As you can see, we have created two Resources here – a Resource Group, and a VNET. These are both very well documented within the AzureRM Provider Documentation – and all the required information and different variables or arguments we could apply are outlined too. The image below shows the details from the Azure RM Provider Documentation Page for a Resource Group:

azurerm_resource_group Registry

Within the image below I’ve shown some of the documented arguments for the Virtual Network Resource:

Example Arguments from Registry

We now have 4 files configured and ready for deployment – in the next post I will cover the planning, testing, deployment and removal of the Resources using Terraform!

Skip to content