In this blog post, I will show you how I use Terraform modules to create a resource group on Microsoft Azure.
Modules
Using modules in Terraform allows us to reuse existing code multiple time and shorten the deployment and configuration time of resources.
Modules are like functions, we call with or without values depending on the configuration.
This post will show you a basic configuration of a Terraform module without passing any variables. In the next post, I will show you how to use variables.
Create a Module
The first step is to create a module. We create a module by creating a folder called Modules, and inside the folder, we create a folder with the module name and place a.TF file. In my case, the structure will look like:
Modules CREATE_AZ_RG rg.tf
Module configuration (rg.tf)
The following code shows how the confirmation. We create an Azure resource group called MyRG in West US region. Since this is a simple example the values are hard coded.
resource "azurerm_resource_group" "rg" { name = "MyRG" location = "westus" }
Main Configuration (main.tf)
In the code below I’m calling the module from the main.tf configuration and only specifying the path to the module using the source value.
terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 2.26" } } } module "azurerm_resource_group" { source = "/Users/Terraform/Modules/AzureRG" }