Step by Step ARM Templates - Helper Functions
@20aman Aug 31, 2016Index of all blogs in this Step by Step ARM Templates series is located here: Step by Step Azure Resource Manager (ARM) Templates - Index
ARM Templates has various dynamic constructs called Helper Functions which can make your template more generic. These constructs reduce the hard coded values in your templates. You can use the information from this blog to make your existing templates more dynamic and start writing new templates with a much generic approach.
Let's look at the most important helper functions and their practical usage one by one.
1. Resource Id - Resource Function
You use this function to determine the ID of a resource. This is only used when the resource (whose ID is needed) is not being deployed in the current template and it already exists in Azure.
The generic syntax to use this is:
resourceId ([subscriptionId], [resourceGroupName], resourceType, resourceName1, [resourceName2]...)
Only required parameters of this helper function are resourceType and resourceName1.
These parameters are as follows:
- subscription ID - This is only needed if you want to refer a different subscription. Default value is the current subscription
- resource Group Name - Name of the resource group where the resource exists. Default is the current resource group, in which you are deploying the template
- resource Type - Type of resource including resource provider namespace
- resource Name 1 - Name of the resource
- resource Name 2 - Next resource name segment if resource is nested. E.g. a VM Extension
Example
"vnetId1": "[resourceId('AE06-Mgmt-RG','Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
"vnetId2": "[resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]"
The above example shows two ways of using the resource ID helper function to determine the Id of a virtual network. First one uses the resource group, resource type and resource name. Second example uses only the resource Type and resource name. Second example assumes the resource group to be same as the template being deployed to.
2. Resource Group - Resource Function
This helper function returns an object that represents the current resource group to which the template is being deployed.
The generic syntax to use this is:
resourceGroup()
No parameters are needed in this helper function.
Example
"vhdStorageName": "[concat('vhdstorage', uniqueString(resourceGroup().id))]",
"storageAccountResourceGroup": "[resourcegroup().name]",
"location": "[resourceGroup().location]"
The above example shows 3 uses of the resource group helper functions. First one uses the ID of the resource group, second uses the name property and third uses the location for the current resource group.
3. Subscription - Resource Function
The generic syntax to use this is:
subscription()
No parameters are needed in this helper function.
Example
"subscriptionId": "[subscription().subscriptionId]"
The above example is straightforward. It fetches the subscription Id of the current subscription.
4. Concat - String Function
This function is used to concatinate (i.e. combine) two or more values.
The generic syntax to use this is:
concat (array1, array2, array3, ...)
At least 1 array is needed for concat to work.
Example
"subnetRef": "[concat(variables('vNetId'), '/subnets/', variables('subnetName'))]"
The above example combines (or concatinates) 3 text values. First value is the value of variable vNetId. Second value is a string "/subnets/". Third value is the value of the variable subnet Name.
These are the most common Helper functions that you will use in 80%-90% of the templates.
To check the complete list of Helper Functions, check this official link: Azure Resource Manager template functions