Exploring The Azure Naming Terraform Module

I recommend for any azure terraform deployments, to use the Azure Naming terraform module for a consistent and easy way to generate azure resource name according to an established naming conventions. I believe it should be a staple in all your azure terraform deployments.

You can read all about it in its repo at https://github.com/Azure/terraform-azurerm-naming and at https://registry.terraform.io/modules/Azure/naming/azurerm/latest.

But I like to give you my point of view and how I like to use it practically and in simple scenarios. Here is a demo.

I start with defining the naming module as such:

module "naming" {
  source  = "Azure/naming/azurerm"
  version = "~> 0.3"
  suffix = "rk"
}

I suggest adding a suffix to differentiate a group of resources that align to a project or solution. “rk” is just my initials. And to use this module is to assign the .name property to the resource name field.

resource "azurerm_resource_group" "this" {
  location = var.location
  name     = module.naming.resource_group.name
}

And I have used this naming module for the other resources.

resource "azurerm_log_analytics_workspace" "this" {
  location            = azurerm_resource_group.this.location
  name                = module.naming.log_analytics_workspace.name
  resource_group_name = azurerm_resource_group.this.name
}
module "avm-res-containerservice-managedcluster" { 
  source              = "Azure/avm-res-containerservice-managedcluster/azurerm" 
  name                = module.naming.kubernetes_cluster.name
...
}
module "avm-res-containerregistry-registry" {
  source  = "Azure/avm-res-containerregistry-registry/azurerm"
  version = "0.4.0"
  name     = module.naming.container_registry.name
  location = azurerm_resource_group.this.location
...
}

You can find the rest of the code here: https://github.com/RoyKimYYZ/az-terraform-cicd/blob/main/avm-aks-example2-tf/avm-res-containerservice-managedcluster.tf

And running this terraform code, it results in the resource group and the resources being named as follow:

Let’s try configuring the Naming module to have multiple suffixes.

After running terraform apply, the resource names results as shown in the Azure portal.

Another technique is to generate unique resource names. Azure requires certain resource names to be globally unique (e.g., storage accounts, web apps). Using unique names ensures there are no conflicts when deploying resources across subscriptions or regions.

So I update every resource name assignment to use the Naming resource name_unique property as such:

resource "azurerm_resource_group" "this" {
  location = var.location
  name     = module.naming.resource_group.name_unique
}

By running the terraform apply, I get the following names generated.

Final Thoughts

I find this naming module really useful and I have shown techniques that I think are applicable most cases. However, by reading the Naming module’s repo’s readme documentation, you will see some more capabilities to fit more sophisticated needs.

Leave a Reply