Mastering Functions in ARM Templates: A Comprehensive Guide

Learn how to use functions in ARM templates!

Anders Moth Falk

Functions in Azure Resource Manager (ARM) templates allow you to add logic and transformations to your templates, making them more flexible and reusable. ARM functions help you perform calculations, manipulate strings, work with arrays, and more, Enhancing your templates' ability to handle complex scenarios by providing advanced logic.

Using Built-in Functions

Azure offers a variety of built-in functions. Here’s a breakdown of commonly used functions:

1.1 String Manipulation

  • concat: Combines multiple strings.
  • "[concat('resource-', parameters('resourceName'))]"
  • toLower / toUpper: Changes string case.
  • "[toLower(parameters('environmentName'))]"
    "[toUpper(parameters('environmentName'))]"

    1.2 Math

  • add, sub, mul, div: Perform basic math
  • "[mul(parameters('instanceCount'), 2)]"

    1.3 Arrays

  • union: Combines arrays.
  • "[union(variables('array1'), variables('array2'))]"
  • Length: Gets array length.
  • "[length(parameters('allowedIPs'))]"

    Ressource ID

  • resourceId: Builds a resource ID based on type and name.
  • "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageName'))]"

    2. Defining Custom Functions

    You can create custom functions within the functions section for repeated operations.
    Example Custom Function
    "functions": [
    	{
    		"namespace": "myNamespace",
    		"members": {
    			"multiplyByTen": {
    				"parameters": [
    					{
    						"name": "input",
    						"type": "int"
    					}
    				],
    				"output": {
    					"type": "int",
    					"value": "[mul(parameters('input'),10)]"
    				}
    			}
    		}
    	}
    ]
    

    3. Calling Functions in the Template

    Invoke a function by referencing is namespace and name:
    "[myNamespace.multiplyByTen(5)]"
    This will return 50 by multiplying 5 by 10.

    With ARM template functions, you can simplify complex deployments, handle data transformations, and create reuseable logic for managing resources dynamically. Combining built-in functions and customer functions allows for flexible, powerful infrastructure-as-code solutions tailored to various Azure deployment needs.