Terraform on Azure: Getting Started

Terraform on Azure: Getting Started

A good place to start with Terraform would be, what is it? Terraform or Infrastructure as code (IaC) in general is a way of describing your infrastructure in code files which can be used to create those resources.

Instead of having to click around a portal or sit at a command prompt running a command at a time you can write out all the infrastructure related to a solution and deploy it all at once.

Terraform is the leading IaC solution as it can be used in multiple cloud environments as well as some on-prem e.g. with VMware.

The below simple example would create a resource group and virtual network.

# Resource Group
resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}


# Virtual Network
resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  address_space       = ["10.0.0.0/16"]
}

Getting Setup

There are a few things we'd need to get installed before creating our first template.

Terraform itself: https://developer.hashicorp.com/terraform/downloads

When you download Terraform on Windows you get a zipped-up .exe file. Just place the .exe in an easily accessible folder e.g. c:\terraform

Once you have the .exe open up your system properties > advanced > environment variables

Within your environment variables in the system variables section at the bottom click on New, then enter terraform as the variable name and then the path to the folder you placed the terraform.exe

To check Terraform is installed correctly you can open a new command prompt and type in the below:

terraform -v

If the terraform environment variable was entered correctly then you'll get a version number back. If you get a command not recognised output check you spelt everything correctly.

Azure CLI: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli

And I'd recommend installing VsCode and the Terraform extension if you don't have these already.

VsCode: https://code.visualstudio.com/

Terraform VsCode extension: https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform


Creating our first Terraform template

Open an empty folder in VsCode and create three new files

main.tf -> This will be the main file we will describe our infrastructure.

vars.tf -> This will be where we'll place the variables used in our main.tf file.

terraform.tfvars -> This will be where we put the values for our variables. We've named this one terraform.tfvars as any file in the same folder as our main.tf file will be automatically loaded when we use our main Terraform file to create our Azure resources.

Open the vars.tf file and paste in the below:

variable "resource_group_name" {
  type = string
  description = "Name for our resource group"
}

variable "location_name" {
  type = string
  description = "Azure region name to deploy our resources to"
  default = "uksouth"
}

variable "network_name" {
  type = string
  description = "The name for the new virtual network"
}

variable "network_address_space" {
  type = list
  description = "A list of addresses to be used in the vNet (10.1.1.0/24)"
}

We have four variables which we'll use in our Terraform template. Each of them has at least the type and a description which will make it easier to understand what the variable is used for.

You might notice we also have a default value for the location_name variable. Having this here means we don't have to provide a location_name as it will default to uksouth we'd only need to specify a value for that variable if we wanted something different.

All of the variable types are strings except for network_address_space which is a list. This is a list because there can be multiple address ranges to be used within a virtual network specified in the CIDR format e.g. 10.1.1.0/24

Open the terraform.tfvars and paste in the below:

resource_group_name = "MyResourceGroup"

network_name = "MyvNet"

network_address_space = ["10.1.1.0/24"]

Then finally open the main.tf file and paste in the below:

# Terraform Provider(s)
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">=3.0.0"
    }
  }
}

# Configuring AzureRM provider
provider "azurerm" {
  features {}
}

# Resource Group
resource "azurerm_resource_group" "example" {
  name     = var.resource_group_name
  location = var.location_name
}

# Virtual Network
resource "azurerm_virtual_network" "example" {
  name                = var.network_name
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  address_space       = var.network_address_space
}

The first section is for our providers. In Terraform there are providers for where you're deploying your infrastructure. In our case, we're deploying to Azure so we will use the AzureRM (Azure Resource Manager) provider.

The next section is configuring the AzureRM provider, for this basic tutorial we're not specifying anything here.

The next two sections you'll notice from earlier are for the resource group and virtual network. We're using our variables using the var.VARIABLENAME format.

You'll also see for the location of the virtual network we're using azurerm_resource_group.example.location, we're able to reuse information provided by other resources like this as we want the virtual network in the same location as the resource group.


Deploying our Terraform template

As long as you've installed Terraform and the Azure CLI you'll now be able to deploy the Terraform template.

Open a PowerShell prompt and enter the below:

az login

This will then prompt you to log into Azure. Once you've done that your PowerShell prompt will come up with tenant and user information.

Next change directory into the location you created your terraform files:

cd LOCATION_PATH_HERE

Now we need to initialise our Terraform deployment which will download our AzureRM provider so we can deploy to Azure.

terraform init

Optionally we can do a plan which will workout what resources will be created, changed or deleted.

terraform plan

From the below example, we can see two resources will be created, the resource group and the virtual network. All looks good to me.

Finally, we apply those changes shown in the plan.

terraform apply

When prompted type in yes and hit enter to confirm the changes.

Now when we check our Azure portal we can see the resources have been created.

Looking at the folder your Terraform files were you'll notice a few new folders/files

The .terraform folder is what was created when we ran the terraform init command, this is where our AzureRM provider was downloaded to and used when we ran the other Terraform commands.

The terraform.tfstate file is the current state of the resources that were deployed. You'll need this file if you want to use the same terraform files to update the resources in the future.

The terraform.tfstate file can be stored in an Azure blob container as a remote state file which will make it easier to share and use going forward, but we'll cover this another day.

The terraform.lock.hcl is a file used during the terraform hcl command to make a list of all dependencies e.g. our AzureRM provider.

Thus is a link to a GitHub repository with the complete Terraform files included.

That's it using Terraform with Azure is that easy.