Getting Started with Terraform

By | 15/04/2021

I have been using Azure ARM templates for a number of months now and managing Infrastructure as Code (IaC) is totally the way forward over using the Azure portal. However, I have started getting lost when looking at nested templates and generating dynamic IDs for automatic Key Vault naming etc. Because of this I have taken the plunge and started investigating Terraform by HashiCorp.

Initially, I have been having difficulties with understand just where to start with Terraform mainly because I currently use Visual Studio Professional 2019 for my ARM templates and there isn’t a plugin for Terraform available – everything is pointing towards Visual Studio Code. However, after a bit more digging, I found the following article the easiest to follow to get started with Terraform:


Pre-requisites

Terraform:

I am using Windows 10 so the installation of Terraform is very simple. In fact, it isn’t installing, rather than downloading the EXE and ensuring the PATH statement points to the right location.

  1. Download the latest version of Terraform from https://www.terraform.io/downloads.html
  2. Extract the EXE from the ZIP file and save to C:\Terraform
  3. Within Environment Settings, add C:\Terraform to the PATH

More info on the above can be found here:

Azure CLI:

As I am currently just concerned with Azure, I needed to install install the Azure CLI. This can either be found here, or opening PowerShell as admin and running the following:

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi

Visual Studio Code Terraform Extension:

I am also using Visual Studio Code as mentioned so it is a good idea to install the HashiCorp Terraform extension:


First Script

Once the pre-requisites have been completed, as per the instructions it was time to create our first piece of infrastructure in Azure with Terraform. The guide shows creating a Resource Group.

From within Visual Studio Code you need to create a new folder and file. The guides shows to create a folder called ‘C:\learn-terraform-azure’ and the initial file must be called ‘main.tf’. Once this is created, paste in the following and save the file:

# Configure the Azure provider
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = ">= 2.26"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "myTFResourceGroup2"
  location = "westus2"
}

Then, from Visual Studio Code’s built in Terminal, run the following – ensuring that you are in the folder ‘C:\learn-terraform-azure’ where the ‘main.tf’ is located:

terraform init

You will see that the required provider (Azure) is downloaded and installed and the folder structure has been populated:

You can now run the following which allows you to check what the script is going to do before you run live:

terraform plan

If this comes back as expected and with no errors you can move onto the following to apply the script – you will be asked to confirm the script by entering ‘yes’:

terraform apply

Conclusion

The above example is all very basic but gives an idea on where Terraform could be useful – being able to run a plan before committing is great for change control for example and not something you can do with regular ARM templates.

Loads more to learn and test. I’ll initially start with trying to migrate my existing ARM templates across to Terraform to get more of an idea how it all works.

Leave a Reply

Your email address will not be published. Required fields are marked *