Deploying Azure resources using Terraform

So, what is Terraform and why do I use it ? Terraform is an infrastructure as code (IaC) tool that allows you to build on-premises or clouds environments with the ability to change and version our deployments. I use Terraform to build my labs for Demonstration and Testing purposes. It allows me to spin up environments relatively fast and destroy them once I no longer need them, this allows me to control the cost associated with Azure resources. In this post I’m going to talk about how you can use Terraform to programmatically deploy resources in Azure and have services up and running within minutes.

Getting setup

Here are a few steps you’ll need to complete beforehand:

  1. First of all, you’re going to need to have Terraform installed, you can find instructions of how to do this here.
  2. Next, you’re going to need an editor, there a many different editors available, I prefer to use Visual Studio Code, link here.
  3. Lastly, but most important of all, patience. Once you get to grips with terraform you’ll love it and I promise it’ll become an addition 😊
Authenticate to Azure

You will need to authenticate to Azure, its important to note that Terraform only supports Azure CLI for authentication. There are various methods that can be used for authentication, using a Microsoft Account via the Cloud Shell or terminal with bash or PowerShell, you can also use a Service Principal should. For purpose of this article we’re going to use a Microsoft Account via a terminal .

1. Start a terminal that has Azure CLI access

2. Login using the command example below.

az login

3. One thing to note is that if you have multiple Azure subscriptions check to see which one you are working with. To check this run the following command. This will show you the default context subscription and ensure you know where you are deploying you resources!

az account show -o table

4. If you would like to see all the subscriptions that your account access too, you can run the following command.

az account list --query "[?user.name=='put_your_email_address_here'].{Name:name, ID:id, Default:isDefault}" --output table

5. If your default context subscription is not the one you want to use, you can run the command below to set the default subscription to the one you require.

az account set --subscription "your_subscription_id"

Creating your Terraform code

1. The first thing we’re going to create in Azure using Terraform is going to be a Resource Group. Create a folder/directory that you wish to host your Terraform code. Examples of the code used in the rest of this article can be found on my GiHub here.

2. Inside that directory, create a file named main.tf. Copy and paste code below into your main.tf file

# Specify resources providers
terraform {
  required_providers {
    azurerm = {
      # Specify what version of the provider we are going to utilise.
      source        = "hashicorp/azurerm"
      version       = ">= 2.90.0"
    }
    random = {
      source        = "hashicorp/random"
      version       = "3.1.0"
    }
  }
}

# Create random string - This will create a random character string to be used for naming of resources
resource "random_string" "rg_random_1" {
  length  = 4
  special = false
}

# Create Resource Group 1
resource "azurerm_resource_group" "rg_1" {
  name     = ${var.rg_name_1}-${random_string.rg_random_1.result}
  location = var.region_1
  tags = {
    Environment = var.tag_environment
    CreatedBy   = var.tag_createdby
    CreatedWith = var.tag_createdwith
  }
}

3. Next, we need to create a file named variables.tf in the same directory. Copy and paste the code below into your variables.tf file.

variable "rg_name_1" {
  description = "Resource Group 1 Name"
  default     = ""
}

variable "region_1" {
  description = "Azure Region"
  default     = ""
}

variable "tag_environment" {
  description = "Resource Group Tags"
  default     = ""
}

variable "tag_createdby" {
  description = "Resource Group Tags"
  default     = ""
}

variable "tag_createdwith" {
  description = "Resource Group Tags"
  default     = ""
}

4. You will notice that the default for all variables has been left blank, with a simple “” placeholder. We are going to be using a .tfvars file toe define the values of those variables. You now need to create a .tfvars file in the same directory, for example, terraform.tfvars. Copy and paste the code below into your terraform.tfvars file.

rg_name_1 = "myLabResourceGroup"
region_1  = "uksouth"
tag_environment = "Lab"
tag_createdby = "Your Name"
tag_createdwith = "Terraform"

5. Next, you will need to create an output.tf file. This file will be used will be used to capture the randomly generated name of the Resource Group for future deployments that need to reference the name of this resource group. Copy and paste the code below into your output.tf file.

output "resource_group_name" {
    value = azurerm_resource_group.rg_1.name
}

Deploying your Terraform code

1. Now that you have created your Terraform code, let’s go a head and deploy it into Azure. First of all we need to initialise the Terraform deployment download the required Terraform modules. To do this, type the command shown below.

terraform init

2. Next we need to create a Terraform plan. This is used to determine what is required to create the configuration you have specified in your Terraform directory. To do this, run the command below.

terraform plan -out main.tfplan

3. Now that you have generated your Terraform deployment plan, we can push the Terraform code into Azure and create our resources. Run the command below to apply your code.

terraform apply main.tfplan

4. That’s it, you have now deployed a Resource Group into Azure using Terraform. To view your deployment you can either go to the Azure portal and view the Resource groups section or you can run the following command in your terminal.

az group show --name <the_name_of_your_resource_group>

5. Now that you have successfully deployed your resource group into Azure, once you have finished with, it’s time to clean it up and remove it. This is quite straightforward, simply run the command below. This will use the tfstate file and destroy all resource that were built using the terraform apply command previously.

terraform destroy
Summary

I hope that this very short blog post about getting started with building resources in Azure using Terraform has been useful to you? I think that the more you use this toolset to deploy infrastructure into Azure, the more you will appreciate its power and simplicity. In my next blog, we’ll build upon this one and add more services into the resource group. Next time, its building Azure VNets 😊

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.