A simple Azure Terraform Walkthrough – Part 2

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: (Click here for 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:
  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 – Planning for our Deployment

From the last post, we already have a folder setup which is prepped with the 4 files we need to deploy resources into Azure using Terraform. If you need to create these files again, please refer to Post 1 in this series, or you can download all 4 files from my GitHub Account here.

To plan for our deployment we need complete a number of actions:
  1. Launch VSCode and browse to the folder containing the Resources we wish to deploy (these are the 4 files we created).
  2. Authenticate to Azure – we will do this using the Azure CLI (which we installed using Chocolatey in Post 1)
  3. Initialise Terraform – this command prepares Terraform for use within our folder.
  4. Run a “Terraform Plan” – this will test our code, and confirm what Terraform will carry out when we apply the configuration.
1. Launching VSCode and opening our folder

This one is a simple step – we just need to launch VSCode, and select “Open Folder…”. Once the Open folder dialogue box pops up, select the folder with the 4 Terraform files in:

VS Code Open Folder

Once the folder is open, you’ll see something similar to the below:

VS Code Files

We are now ready to Authenticate to Azure!

2. Authenticating to Azure

To authenticate to Azure, we will utilise the Azure CLI that we installed in Post 1 (using Chocolatey). The great news here is that we can do all of this within VSCode easily! To get started, select Terminal -> New Terminal from the VSCode Menu:

VS Code Terminal

We will then be shown a Terminal window:

VS Code Terminal

Within this Terminal window, type the following command:

az login

VS Code Terminal

This will start the authentication process to Azure, and you will be presented with a browser window where you should authenticate to your Azure environment. Once this is completed, you will see the following:

Azure Login

We can now return to VSCode, and this will show the Subscriptions we have access to. If you need to select a specific Subscription, use the following command:

az account set --subscription "subscription name"

We are now authenticated to Azure, and we can move onto initialising Terraform!

3. Initialising Terraform

Initialising Terraform is the process by which we prepare a directory that contains Terraform configuration for use. This process sets up Terraform within the directory, and downloads any required providers (as defined by our provider.tf). You can read more about the initialisation here.

To initialise Terraform, run the following command within the VSCode Terminal window:

terraform init

You will then see an output like below:

VS Code Terraform Init

We have now initialised Terraform, and we are ready to run a Plan!

4. Running Terraform Plan

Before we carry out our deployment, it’s important that we run a Plan command. Terraform Plan allows us to see the execution Plan Terraform will use to create our infrastructure. When we run Plan, the following is carried out:

  1. Terraform reads the current state of any existing remote objects, to ensure the state file is up to date.
  2. Compares the current configuration to the prior state and notes any differences.
  3. Proposes a set of changes to bring the remote infrastructure into line with the defined configuration.

You can read more about Terraform Plan here.

To run Terraform plan, type the following command into the VS Code Terminal:

terraform plan

This will then output the following within VSCode:

VS Code Terraform Plan

As you can see, Terraform is confirming with the Plan above that it intends to create two objects:

  1. A Resource Group called “rg-demo-lab” within the UK South Azure Region
  2. A Virtual Network called “region1-vnet1-hub1” using an IP address space of “10.10.0.0/16” within the above Resource Group.

This confirms that Terraform is going to create the Resources we expect (based on our azuredeploy.tf) from the previous post! We are now ready to move onto deployment!

Step 2 – Deploying Resources with Terraform

We are now ready to deploy our Resources using Terraform, and have already confirmed that our execution plan is as we’d expect – so, we can now move onto the apply command! You can read more about the Apply command here.

To start this process, run the following:

terraform apply

This will then provide an output as shown below:

VS Code Terraform Apply

As you will see, Terraform Apply also runs a plan phase – and gives the same output as a Plan, whereby Resources to be created (or changed or destroyed) are also shown. To progress to deployment, we just need to enter “Yes“, press enter, and then Terraform will deploy the Resources:

VS Code Terraform creation

Our Terraform deployment has now been completed – and Terraform has deployed two Resources for us. In the next section, we will check these Resources using the Portal.

Step 3 – Testing

Whilst our deployment here is simple, it’s still important to check that our Resources have been created successfully – we can do this in the Azure Portal:

Resource Groups

VNETs

We can also confirm this using the Azure CLI, by running the following command:

az resource list

As you can see from the VSCode screenshow below – we have the expected Resources:

VS Code Resources

As you can see, the Resources have been created successfully! Next up, we will destroy the Resources!

Step 4 – Destroying Resources

Destruction of Resources in Terraform is also an easy process – and therefore one that needs to be used carefully! In this post, we will remove the Resources we created, and then confirm that they have been removed using the Portal. You can read more about the Destroy command here. To start this process, run the following command:

terraform destroy

This will then output the following within VSCode:

VS Code Terraform Destroy

As you can see, once again the output is similar to the Terraform Plan section – this is because again Terraform runs a Plan to provide an overview of the work that will be carried out. To confirm the Destruction of Resources, we just need to enter “Yes” and then press Enter. Terraform will then Destroy the Resources:

VS Code Terraform Destroy

We’ve now completed the removal of the Resources using the Terraform Destroy command – and a quick check in the Portal will confirm this:

No Resources

We can also confirm again using the Azure CLI command “az resource list” if required!

We’ve now completed Planning, Deployment, and Destruction of Resources using Terraform – in the next post I will cover some tips, tricks and next steps for using Terraform with Azure!

Skip to content