Azure provides a helpful number of functions which can be used in ARM templates. It makes our life easier.
We can see the complete list of Azure ARM function here
Apart from that in some situations, you may find your self where you need to implement custom function inside ARM templates. So we can reuse it. So it is DRY.
Typically, we use complicated logic inside the function that we don’t want to duplicate in the ARM template.
So is it possible in ARM template? Yes, ARM templates give us the opportunity to implement custom functions. š
Keep in mind. There are some restrictions when we use functions as below.
- 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.
- Parameters for the function can’t have default values
The custom function should be declared inside functions property in an ARM template.
Following is a sample function which accepts the container name as a parameter and appends resource group location to container name.
{ | |
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", | |
"contentVersion": "1.0.0.0", | |
"functions": [ | |
{ | |
"namespace": "rt", | |
"members": { | |
"containerNameWithLocation": { | |
"parameters": [ | |
{ | |
"name": "containerName", | |
"type": "string" | |
} | |
], | |
"output": { | |
"type": "string", | |
"value": "[concat(parameters('containerName'), resourceGroup().location)]" | |
} | |
} | |
} | |
} | |
], | |
"parameters":{ | |
"containerName": { | |
"type": "string", | |
"defaultValue": "blobcontainer" | |
} | |
} | |
} |
Note that parameters used in scope of function should be declared in parameters section in ARM template.
Your functions require a namespace value to avoid naming conflicts with template functions. In above function we have used rt as namespace.
Then we can call the function as below
rt.containerNameWithLocation(parameters(‘containerName’))
Following snippet shows actual usage of this function.
"resources": [ | |
{ | |
"name": "[parameters('storageAccountName')]", | |
"type": "Microsoft.Storage/storageAccounts", | |
"apiVersion": "[variables('storageAccountApiVersion')]", | |
"location": "[parameters('location')]", | |
"properties": { | |
"accessTier": "[parameters('accessTier')]", | |
"supportsHttpsTrafficOnly": "[parameters('supportsHttpsTrafficOnly')]" | |
}, | |
"dependsOn": [], | |
"sku": { | |
"name": "[parameters('accountType')]" | |
}, | |
"kind": "BlobStorage", | |
"resources": [ | |
{ | |
"name": "[concat('default/', rt.containerNameWithLocation(parameters('containerName')))]", | |
"type": "blobServices/containers", | |
"apiVersion": "[variables('storageAccountApiVersion')]", | |
"dependsOn": [ | |
"[parameters('storageAccountName')]" | |
] | |
} | |
] | |
} | |
], |
So enjoy writing custom functions in ARM Template š