Skip to main content

Name Templater module

A module to generate resource name template.

GitHub Logo Terraform Logo


There are situations where simple name prefixing is not enough. More complex structures are required. This module generates a string template that can be used with Terraform's format() function to generate the actual resource name.


A simple module invocation might look like the following:

module "name_templates" {
source = "../../modules/name_templater"

resource_type = "vnet"
name_template = {
delimiter = "-"
parts = [
{ prefix = null },
{ bu = "rnd" },
{ randomize = "__random__" },
{ env = "prd" },
{ name = "%s" },
{ abbreviation = "__default__" },
name_prefix = "a_prefix"

The value the module will output for such invocation would be "a_prefix-rnd-crediblefrog-prd-%s-vnet".

As you can see:

  • all parts values are glued together to form a template name

  • the prefix key is just a placeholder that eventually is replaced with the value of name_prefix

  • the __random__ string is replaced with a name of a random pet (in case you need to randomize some name, for testing purposes for example)

  • the __default__ string is replaced with a resource abbreviation.

    This abbreviations are defined with var.abbreviations variable. The module contains basic abbreviations following Microsoft suggestions, but they can be overriden with custom definitions.

    The important part is that the resource_type has to match an entry in abbreviations variable, otherwise the abbreviation will be replaced with an empty string.

To create the actual resource name the following code can be used:

vnet_name = format(module.name_templates.template, "transit")

Following the values above the actual resource name would be "a_prefix-rnd-crediblefrog-prd-transit-vnet".



terraform>= 1.2, < 2.0
random~> 3.5


random~> 3.5


No modules.




resource_typeA type of resource for which the name template will be created. This should follow the abbreviations resource naming standard.stringn/ayes
name_prefixPrefix used in names for the resources.stringn/ayes
name_templateA name template definition.

Consist of two elements:

- parts - a list of elements that will form the template name
- delimiter - a string that will be used to separate the elements.

There are couple of rules to be followed:

- the order DOES matter
- parts is a list of single element maps
- keys in parts elements will be dropped, they are only informational, only values will be used
- value for the prefix key will be replaced with the var.name_prefix value
- a value of __default__ will be replaced with an abbreviation defined in the var.abbrevations and matching var.resource_type.
- since this module generates template name do REMEMBER to include a part with %s value

default = {
default = {
delimiter = "-"
parts = [
{ prefix = null },
{ bu = "rnd" },
{ env = "prd" },
{ name = "%s" },
{ abbreviation = "__default__" },
storage = {
delimiter = ""
parts = [
{ prefix = null },
{ org = "palo" },
{ env = "prd" },
{ name = "%s" },
delimiter = string
parts = list(map(string))
abbreviationsMap of abbreviations used for resources (placed in place of "__default__").

These abbreviations are based on Microsoft suggestions.
"application_gw": "agw",
"application_insights": "appi",
"availability_set": "avail",
"bastion": "bas",
"data_disk": "disk",
"file_share": "share",
"load_balancer": "lb",
"log_analytics_workspace": "log",
"managed_identity": "id",
"nat_gw": "ng",
"network_interface": "nic",
"nsg": "nsg",
"nsg_rule": "nsgsr",
"os_disk": "osdisk",
"public_ip": "pip",
"public_ip_prefix": "ippre",
"resource_group": "rg",
"route_table": "rt",
"service_endpoint": "se",
"storage_account": "st",
"subnet": "snet",
"udr": "udr",
"virtual_machine": "vm",
"virtual_machine_scale_set": "vmss",
"virtual_network_gateway": "vgw",
"vnet": "vnet",
"vnet_peering": "peer"


templateA template string that can be used with format function to generate a resource name.