Technical Thursday – IF statements in ARM template

Today I would like to show you a cool feature of ARM Template which “changed the world”. Although this feature has been available since last September I felt I must say some words about it.

In the past when we wanted to make a little bit complex solution where we had to choose one or more options during deployment – according to the parameters or Customer’s requirements – we had to create separated ARM Templates for each scenario such as create a Standard or Premium storage account according to the chosen VM size.

Then Microsoft provided the real Logical functions for Azure Resource Manager templates and we realized the life became easier.

Nevertheless the documentation says the usage is simple – if(condition, trueValue, falseValue). If you start to make nested IF statements you would be struggling with the syntax. Therefore I would like to provide you a useful ARM Template and example which makes you life easier in this area.

Usage in template

Regarding the usage the documentation is quite simple so here is a simple example for usage in “variables” section. In this example I would like to show you how can you check that whether a string contains “s” or “S”. (the “s” inside the VM size name means that is SSD ready)

"storageAccountType": "[if(contains(variables('vmSizeProfile'), 'S'), 'Premium_LRS', if(contains(variables('vmSizeProfile'), 's'), 'Premium_LRS', 'Standard_LRS'))]",

Explanation

Hard to read, doesn’t it?…Here is the explanation in C#

if(vmSizeProfile.Contains("S")) {
	/* Executes when the vmSizeProfile contains capital S */
	if(vmSizeProfile.Contains("s")) {
	/* Executes when the vmSizeProfile contains lowercase s */
	storageAccountType = "Premium_LRS";
	} else {
		/* Executes when the vmSizeProfile does not contain lowercase s */
		storageAccountType = "Standard_LRS";
} else {
	/* Executes when the vmSizeProfile does not contain capital S */
	storageAccountType = "Standard_LRS";
}

I hope it helps.

Example

Finally I would like to provide you a working solution which can deploy a Storage Account according to the chosen Virtual Machine size. (I know we already use manage disks 🙂 )

You can download this ARM template from my git and test whether is Premium storage account.

 

Feel free to get in touch with any further question…