User-defined functions in ARM template
Within your template, you can create your own functions. These functions are available for use in your template. User-defined functions are separate from the standard template functions that are automatically available within your template. Create your own functions when you have complicated expressions that are used repeatedly in your template.
This article describes how to add user-defined functions in your Azure Resource Manager template (ARM template).
Define the function
Your functions require a namespace value to avoid naming conflicts with template functions. The following example shows a function that returns a unique name:
"functions": [
{
"namespace": "contoso",
"members": {
"uniqueName": {
"parameters": [
{
"name": "namePrefix",
"type": "string"
}
],
"output": {
"type": "string",
"value": "[concat(toLower(parameters('namePrefix')), uniqueString(resourceGroup().id))]"
}
}
}
}
],
Use the function
The following example shows a template that includes a user-defined function to get a unique name for a storage account. The template has a parameter named storageNamePrefix
that is passed as a parameter to the function.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageNamePrefix": {
"type": "string",
"maxLength": 11
}
},
"functions": [
{
"namespace": "contoso",
"members": {
"uniqueName": {
"parameters": [
{
"name": "namePrefix",
"type": "string"
}
],
"output": {
"type": "string",
"value": "[concat(toLower(parameters('namePrefix')), uniqueString(resourceGroup().id))]"
}
}
}
}
],
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "[contoso.uniqueName(parameters('storageNamePrefix'))]",
"location": "South Central US",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"supportsHttpsTrafficOnly": true
}
}
]
}
During deployment, the storageNamePrefix
parameter is passed to the function:
- The template defines a parameter named
storageNamePrefix
. - The function uses
namePrefix
because you can only use parameters defined in the function. For more information, see Limitations. - In the template's
resources
section, thename
element uses the function and passes thestorageNamePrefix
value to the function'snamePrefix
.
Limitations
When defining a user function, there are some restrictions:
- The function can't access variables.
- The function can only use parameters that are defined in the function. When you use the parameters function within a user-defined function, you're restricted to the parameters for that function.
- The function can't call other user-defined functions.
- The function can't use the reference function or any of the list functions.
- Parameters for the function can't have default values.
Next steps
- To learn about the available properties for user-defined functions, see Understand the structure and syntax of ARM templates.
- For a list of the available template functions, see ARM template functions.