Use Terraform Modules And Variables With Microsoft Azure

In this blog post, I will show you how I use Terraform modules with variables to create resources in Microsoft Azure.


In yesterday’s blog post I showed how to use modules without variables and hardcode the values. Today we will take a step forward and make the module more usable.

Folder and Files

Below is the folder and file structure I am going to use, the only difference you will find here compared basic configuration is that I’m also using a file which will hold the variables.


Below, is the content of the file. as you can see I have two variables that I will use to create a Resource Group in Azure.

variable "rgname" {
    type = string

variable "rglocation" {
    type = string

The content of the which holds the module looks as follow:

resource "azurerm_resource_group" "rg" {
  name     = var.rgname
  location = var.rglocation

The last piece of the puzzle is the file which I will use to call the module. In the file, I set the values for the resource group (name and location). If you notice I’m using the variables name I have specified in the file.

module "azurerm_resource_group" {
    source = "/Terraform/Modules/AzureRG2"
    rgname = "Myrg0001"
    rglocation = "westus"