Azure Terraform: Part 3 – Resource Group

Reading Time: 4 minutes

Part 3 of our Terraform journey takes us into the world of defining and deploying resources. The starting point of deploying any resource within Azure is to create a container that will hold your resources. In Azure this container is called a resource group.

Lets begin by understanding what a resource group is,

Azure Resource Group

Like any good magic story you need a vessel to hold all the magic. A resource group is that container that holds all the Azure magic, cloud resources you deploy. They provide a logical container where your virtual machines, networks, and other Azure entities harmoniously coexist.

You can create multiple Azure resource groups, but as we are at the beginning of our magical journey we will create one to hold all the resources we will define and create.

Now, let’s compose our first stanza with Terraform.

Deploy An Azure Resource Group

From within the Terraform documentation for Azure you can search for the resources you want to deploy,

The documentation will provide you with sample code and argumentations you can define, required and optional.

For our example we will define the following resource group,

resource "azurerm_resource_group" "ftrg001" {
  name     = "FT23-RG-001"
  location = "uksouth"

  tags = {
    environment = "dev"
  }
}

The code above can be understood as follows,

  • Resource: azurerm_resource_group is the resource we are creating. ftrg001 is the name given to the resource and how you reference it within your code (more on that later)
  • Name: The name given to the resource group when created within Azure
  • Location: The region it will be deployed in

Copy the code from the code code block above and add it to your main.tf file within Visual Studio Code, save and run terrafrom init to initialise Terraform.

This maybe a good point to do a quick overview of the Terraform commands we will be using on our journey.

Terraform Commands Overview

As part of our Terraform journey we will make extensive use of the following 5 commands,

  • terraform init : Initialises the backend, prepares your working directory allowing you to run other commands
  • terraform validate: Checks if your code is valid
  • terraform plan: Checks and shows changes that will be made to your configuration
  • terraform apply: Applies the changes you have defined within your code
  • terraform destroy: Destroys the infrastructure you have created

For a detailed explanation and a list of other commands visit the Terraform Basic CLI Features page here

Login to Azure

To login to azure type az login in the terminal within Visual Studio Code, you will be re-direct via your web browser to login

When you have successfully logged in you will see the following within the browser Window

We will use the cached login credentials to validate and to apply our Terraform configuration. This is not the best approach for a production environment but will suffice for the beginning of our journey.

Terraform Validate

To validate your configuration type terraform validate in the terminal window,

If there are no issues with your configuration you will get back a success.

Terraform Plan

Next run terraform plan,

As you can see from the output in the terminal window the plan confirms the creation of 1 resource group named FT23-RG-001.

I have also confirmed an optional argument tags, where the resource will be tagged as being part of the dev environment.

Terraform Apply

To deploy the resource run terraform apply, you will be asked to confirm the creation of the resource before the resource is created,

Once it has successfully run, it will confirm the creation of the resource completed sucessfully.

Confirm Resource Group Creation

To confirm the resource group created successfully run the following AZ command from the terminal within Visual Studio Code,

  • az group list –output table

You can also confirm this by logging into the Azure Portal

This basic script, a mere prologue for what is to come sets the tone and delivers some invaluable lessons we shall carry forward as we define and deploy more resource into our container.

In the next adventure we will deploy a VNET and a Subnet into our resource group. Happy orchestrating.