Azure Resource Manager Templates – Working with variables, parameters, functions and loops.

April 7, 2017 By Introductions No Comments

In our introduction to Azure Resource Manager Templates we wrote a template from scratch. Whenever we used a name for a resource we had to use the full name. Imagine having to change this value. Sure you could find and replace all instance, but we all know how this could go wrong.

For a basic introduction also see our previous article on Azure Resource Manager Templates 101.


Of course ARM has a mechanisms to deal with this. Under the variables property you can define key value pairs. Later you reference the value by it’s key. That way when you want to change the value you only have to do it once.

To reference the value use the variables function.

Also you can compose variables using other variables and functions.

Deployment parameters

Variables centralize the values you have to use throughout your template. But they still live in your deployment files. Imagine you want to deploy the same set of resources for a production, a staging and a development environment. Ideally working from one master deployment file.

You can achieve this by using ARM parameters. You can supply the values for these parameters from a file, via the command line or the user interface.

The parameters property accepts a set of parameter names and the type expected.

Type is the only required property for every type. To provide a description of the parameter you add the metadata property as shown. This available for any type.

Available types are:

  • string (i.e. “alice”),
  • int (a number 42),
  • bool (i.e. true or false),
  • object (a JSON notation, i.e. { firstName: “alice”, lastName: “doe” }) or
  • array (a JavaScript array, i.e. [1,2,3] or [“a”,”b”,”c”]).

The minLength and maxLength properties are available for string and array. For the latter is defines how many values must be provided at least and at most.

If you want to restrict the values a parameter may have, use the property allowedValues. You can specify a default using property defaultValue. This works for string and int.

Using parameters is analog to variables:


Deploying with parameters

You can specify each parameter in the PowerShell command.

You can also create a parameters file. The contents are a similar to a resource definition file. But they contain only the schema, contentVersion and parameters properties.

Then you run the PowerShell command using the TemplateParameterFile parameter.

To load the file from a remote URI instead, use the TemplateParameterUri  parameter.

See Microsoft Docs for more information on deploying Azure Resource Manager Templates.


You can use a number of functions when setting values in templates. Some of the most used ones are:

  • int(“1”) – converts your string to an integer
  • add(1,2), mul(1,2), sub(2,1), div(4,2) – simple arithmetic operations
  • concat(“abc”,”def”) or concat([a,b,c],[d,e,f]) – puts together two strings or arrays
  • replace(“a.b.c”, “.”, “-“) – replaces parts of strings, e.g. the result here is “a-b-c”.
  • split(“a.b.c”, “.”) – splits a string into an array, e.g. the result here is [“a”,”b”,”c”]
  • substring(“azure”,2,1) – returns a string with a specific length from another string, e.g. the result here is “u”.
  • toLower,toUpper – makes strings upper or lower case
  • uniqueString(“a”,”b”,”c”,”d”) – creates a hash of the input values, e.g. resource group, parent name or even date

Be aware that you cannot use functions (or any dynamic expression) in parameter files.

See Microsoft Docs for more information on Azure Resource Manager Template functions.


If you want to create a resource more than one time there are two options. You can copy paste the resource definition. But what happens if you want a parameter to decide how often to create the resource.

For this you can use the special “copy” property on the resource definition.

Note the copyIndex function used as a suffix in the name. This add a unique number to the resource name. Otherwise the second copy would fail because names must be unique. This will number the site something0, something1, … . If you would rather start with 1 simply use copyIndex(1).

You cannot use copy on child resources but rather only on top-level resources.

Another helpful usecase is using copy together with an array of names. Assume for example you want to create 3 identical resources but with different names.

You pass an array of countries in as a parameter. The copy count then uses the length function to determine how many items were passed. Then the suffix for the name is read from that same array using copyIndex.

See Microsoft Docs for more information on loops in Azure Resource Manager Templates.