Azure Naming Convention Automated

Summary

To increase the manageability of Azure resources, we need to have a naming convention for all resources. This document describes the naming convention for Azure resources and how to automate the creation of Azure resources using a powershell module.

Naming Convention

Azure Resource Group

The naming components for Azure resource groups is as follows:

Environment: The environment the resource group is deployed to. This can be one of the following:

  • bt: Build environment
  • dt: Development environment
  • it: Integration Test environment
  • at: Acceptance environment
  • ut: User Acceptance Test environment
  • pp: Pre-Production environment
  • pd: Production Diagnostic environment
  • pr: Production environment

Region: The region the resource group is deployed to. This can be one of the following:

  • aue: Australia East
  • aus: Australia Southeast
  • euw: West Europe
  • eus: East US
  • sea: Southeast Asia
  • weu: West US
  • etc...

System: The system the resource group is deployed to. This must be a 3 letter acronym for the system.

SubSystem: The subsystem the resource group is deployed to. This must be a 3 letter acronym for the subsystem.

Suffix: The suffix for the resource group. Suffix must not be "RG"

Because the resource group name has a fixed length for the mandatory components it makes it easier to calculate a name of the resource group.

<environment>-<region>-<system>-<subsystem>-<suffix>
or
<environment>-<region>-<system>-<suffix>
or
<environment>-<region>-<system>

Examples Resource Group

    Environment: dt
    Region: aue
    System: abc
    SubSystem: xyz
    Suffix: 123
    Resulting Resource Group Name: DT-AUE-ABC-XYZ-123

    Environment: dt
    Region: aue
    System: abc
    Resulting Resource Group Name: DT-AUE-ABC

Azure Resource

The naming components for Azure resources is the same as the Azure resource group it is deployed to except the hyphens are removed and the last four characters of the subscription id is suffixed to the name. And is all lowercase. The subscription id is used to ensure the resource name is globally unique. In the case where the resource name is not unique, the resource group name should be suffixed with a number starting at 1 and incrementing until the name is unique.

#Depending on the resource group name the resource name can be one of the following:
<environment><region><system><subsystem><suffix><subscriptionidlast4digits>
or
<environment><region><system><suffix><subscriptionidlast4digits>
or
<environment><region><system><subscriptionidlast4digits>

Examples Resource

Resource Group Name: DT-AUE-ABC-XYZ-XXX and Subscription Id: 12345678-1234-1234-1234-123456789012 Resulting Resource Name: dtaueabcxyzxxx9012

Naming Module

The following code can be copied and pasted into a new file and saved as AzureNamingConvention.psm1. This module can then be imported into a powershell session using the Import-Module command. Please see the usage examples in the comments for the module.

<#
.SYNOPSIS
    Module for creating resource group names and resource names in a standardised way in Azure
.EXAMPLE
    New-DefaultResourceGroupName -Environment "st" -Region "aue" -SystemName "abc" -SubSystemName "xyz" -Suffix "1"
    New-DefaultResourceName -ResourceGroupName "ST-AUE-ABC-XYZ-1"

.DESCRIPTION
    The function New-DefaultResourceGroupName creates a default resource group name for an Azure resource. The function takes five parameters:
        Environment: This is a mandatory parameter that takes a string value and is validated against a set of predefined values (dt, st, ut, pr). It represents the environment in which the resource will be deployed.
        Region: This is a mandatory parameter that takes a three-character string value and represents the Azure region where the resource will be deployed.
        SystemName: This is a mandatory parameter that takes a three-character string value and represents the name of the system or application to which the resource belongs.
        SubSystemName: This is an optional parameter that takes a three-character string value and represents a sub-system or module within the system.
        Suffix: This is an optional parameter that takes a string value and represents a suffix to add to the resource group name.
    The function then constructs the resource group name based on the parameters provided. If the SubSystemName and Suffix parameters are not provided,
    the function constructs the resource group name by concatenating the Environment, Region, and SystemName parameters. If the Suffix parameter is provided, it is appended to the end of the resource group name.
    If the SubSystemName parameter is provided, it is appended to the end of the resource group name, separated by a hyphen. If the resource group with the generated name already exists, an error is thrown.
    If multiple resource groups with the same name are found, the function adds a numeric suffix to the name to ensure uniqueness. The function then returns the generated resource group name.

    The New-DefaultResourceName function generates a default name for an Azure resource based on the provided resource group name. The function takes one parameter:
        ResourceGroupName: This is a mandatory parameter that takes a string value representing the name of the Azure resource group.
    The function then generates a default name for an Azure resource by removing any hyphens from the resource group name and converting the resulting string to lowercase. This generated name is returned by the function.
    For example, if the ResourceGroupName parameter is set to "my-resource-group", the function would generate the default name "myresourcegroup". This function is useful for generating default names for
    resources that are created within a resource group, where the resource name is based on the resource group name.

.NOTES


.LINK
    https://docs.driesventer.com
#>

#region New-DefaultResourceGroupName
function New-DefaultResourceGroupName {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)][ValidateSet('bt', 'dt', 'st', 'it', 'ut', 'pp', 'pd', 'pr')][string]$Environment,
        [Parameter(Mandatory = $true)][ValidateLength(3, 3)][string]$Region,
        [Parameter(Mandatory = $true)][ValidateLength(3, 3)][string]$SystemName,
        [Parameter(Mandatory = $false)][ValidateLength(3, 3)][string]$SubSystemName,
        [Parameter(Mandatory = $false)][string]$Suffix
    )
    if (!$SubSystemName -and !$Suffix) {
        $baseName = ($Environment + "-" + $Region + "-" + $SystemName).ToUpper()
    }
    elseif (!$SubSystemName -and $Suffix) {
        $defaultResourceGroupName = ($Environment + "-" + $Region + "-" + $SystemName + "-" + $Suffix).ToUpper()
    }
    elseif ($SubSystemName -and !$Suffix) {
        $baseName = ($Environment + "-" + $Region + "-" + $SystemName + "-" + $SubSystemName).ToUpper()
    }
    elseif ($SubSystemName -and $Suffix) {
        $defaultResourceGroupName = ($Environment + "-" + $Region + "-" + $SystemName + "-" + $SubSystemName + "-" + $Suffix).ToUpper()
        #check if the resource group already exists
        if (Get-AzResourceGroup -Name $defaultResourceGroupName -ErrorAction SilentlyContinue) {
            throw "Resource group with name $defaultResourceGroupName already exists. Please use a different suffix."
        }
    }
    if ($baseName) {
        $defaultResourceGroupName = ($baseName + "-" + "1").ToUpper()
        $count = 1
        while (Get-AzResourceGroup -Name $defaultResourceGroupName -ErrorAction SilentlyContinue) {
            $count++
            $defaultResourceGroupName = ($baseName + "-" + $count).ToUpper()
        }
    }
    return $defaultResourceGroupName
}
#endregion New-DefaultResourceGroupName

#region New-DefaultResourceName
function New-DefaultResourceName {
    param (
        [Parameter(Mandatory = $true)][string]$ResourceGroupName
    )
    #get last 4 digits from subscription id
    $subscriptionIdSuffix = ((Get-AzContext).Subscription.Id).Substring($subscriptionId.Length - 4)
    $baseName = $ResourceGroupName.Replace("-", "")
    $defaultResourceName = ($baseName+$subscriptionIdSuffix).ToLower()
    return $defaultResourceName
}
#endregion New-DefaultResourceName

#region Exported Functions
# public (exported)
Export-ModuleMember -function New-DefaultResourceGroupName
Export-ModuleMember -function New-DefaultResourceName

#endregion Exported Functions

Code Repository

The up to date code is available here Powershell-Modules/Governance.Naming