Use Variables With Terraform and Azure

In this blog post about Terraform, I will show you how to use Terraform variables with hardcode names and other configuration in blocks.

Variables

Using variables allows building configuration that is not configured using hardcode values and also prepare us to get used to using modules that save time and effort.

When it comes to Terraform variables, the configuration is straightforward but very powerful, and you will see.

In my case, we will create a Resource Group in Azure using a variable that will hold the name of the resource group.

Variable Configuration

Below is the block of the variable I’m creating. The configuration starts with the word variable and the value (default). We can also specify the type (string, int, etc.).

variable "rgname" {
    default = "Myrgname" 
} 

Configuration

The code below will put everything together and will create a resource group using the variable value.

resource "azurerm_resource_group" "rg" {
  name     = var.rgname
  location = "australiasoutheast"
}

To view the entire code, please visit our GitHub repository.

Leave a Reply

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