Automatically Naming Resources in an Azure Resource Management Template

Why?

Resource templates are a great concept, but are fraught with danger. Badly named resources and naming inconsistency across resources in different subscriptions can make it difficult to determine purpose.

If a resource such as a Virtual Network or a Storage Account is badly named in a resource template file, you may find that you have to rely on the icon pictures in the Azure portal to indicate what type of resources you are looking at.

What if you need to see at a glance:

  • What region the resource belongs to i.e. Australia East or Australia Southeast?
  • What environment the resource belongs to i.e. Production or Testing?
  • What type of resource it is?

The problem comes from trying to keep the names meaningful and consistent. How do you ensure that your naming standards are adhered to?

Scenario

In my case I would like to name resources as follows:

<Resource Prefix>_<Environment>_<Location>

For instance:

"Vnet_Prod_auSoutheast"

Solution

Create new parameters (under the parameters section) in the main JSON (AzureDeploy.json) file for:

  • Current location (i.e. “Australia East”)
  • Short form of the location (i.e. “auEast”)
  • Environment (i.e. “Prod” or “Testing”)
"location" : {
    "type" : "string",
    "allowedValues" : ["Australia East", "Australia Southeast"],
    "metadata" : {
        "description" : "Deployment location"
    }
},
"shortlocation" : {
    "type" : "string",
    "defaultValue" : "[replace(resourceGroup().location,'australia','au')]",
    "metadata" : {
        "description" : "Short Deployment location"
    }
},
"Environment" : {
    "type" : "string",
    "allowedValues" : ["Prod", "Testing"],
    "metadata" : {
        "description" : "Production or Testing"
    }

Create additional parameters (under the parameters section) in the main JSON file to define the naming convention prefixes (In this case for Virtual Networks and Subnets):

"VNETNamingStandard" : {
    "type" : "string",
    "defaultValue" : "VNet_",
    "metadata" : {
        "description" : "VNet Naming Standard"
    }
},
"SubnetNamingStandard" : {
    "type" : "string",
    "defaultValue" : "Subnet_",
    "metadata" : {
        "description" : "Subnet Naming Standard"
    }
}

Use the ‘Concat’ command in the main JSON file (under the parameters section)  to create new standardized names by concatenating the prefix, environment and short location :

"vnetName" : {
    "type" : "string",
    "defaultValue" :
    "[concat(parameters('VNetNamingStandard'),parameters('Environment'),'_',  parameters('shortlocation'))]",
    "metadata" : {
        "description" : "VNet name"
    }
},
"SubnetName" : {
    "type" : "string",
    "defaultValue" :
    "[concat(parameters('SubnetNamingStandard'),parameters('Environment'),'_',  parameters('shortlocation'))]",
    "metadata" : {
        "description" : "Subnet name"
    }
}

For Storage accounts it’s a little trickier. They have to be lowercase.

"StorageName" : {
    "type" : "string",
    "defaultValue" : "[concat(tolower(parameters('StorageNamingStandard')),tolower(parameters('Environment')),tolower(parameters('shortlocation')))]",
    "metadata" : {
        "description" : "Storage Name"
    }
}

Note: There maybe a better way to make the whole string lower case, I haven’t found it yet!

Parameters JSON file vs Main Deployment JSON file

I advise that the above is placed into the primary azuredeploy.json file. This frees up the Azuredeploy.parameters.json file for items that differ in each deployment i.e. the Address Space for the Virtual Network, different environment etc.

End Note

It is easy to write a basic JSON template for Resource deployments. But it is a task fraught with danger if resources are not systematically named and deployed. Large scale deployments require careful planning.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s