How the if() function executes in Azure Resource M

2019-07-18 22:56发布

问题:

I am using if() functions in my ARM template to conditionally set some connection string values in my Web App resource. The current condition looks like this.

"[if(equals(parameters('isProduction'), 'Yes'), concat(variables('redisCacheName'),'.redis.cache.windows.net:6380|', listKeys(resourceId('Microsoft.Cache/Redis', variables('redisCacheName')), '2015-08-01').primaryKey, '|', variables('resourcePrefix')), parameters('redisSessionStateConnection'))]"

To simplify it, the condition looks like this;

[if(equals(arg1, arg2), true_expression, false_expression)]

When I deploy the ARM template with isProduction parameter set to No the execution throws an exception. When isProduction parameter is set to Yes then the template works fine. The exceptions is related to ARM trying to find the redis cache resource which will not be deployed in non production environment.

My guess is even if the isProduction parameter value is No, the true_expression in the above condition which is referencing the Redis Cache resource is getting executed and since the Redis Cache resource is not created in a non production state, it throws the exception.

So my question is, when we have a condition like above, will the true_expression and the false_expression in the if() function is evaluated before the actual condition of the if() function is executed?

If so what would be possible workarounds to get around this problem?

回答1:

Both sides of if() are evaluated regardless (in ARM templates). so you have to work around that using "clever" ways.

you could use nested deployments\variables to try and work around that.

update: this has been fixed some time ago, only the relevant part of if() function is evaluated.



回答2:

My guess would be: no, only the yes, both expressions needed based on the outcome of the if statement is are evaluated.

To solve your issue: you can use environment specific parameter files. This enables you to only include parameters for the environment you're deploying to.

See the documentation on parameters in the 'Understand the structure and syntax of Azure Resource Manager templates' article.

In the parameters section of the template, you specify which values you can input when deploying the resources. These parameter values enable you to customize the deployment by providing values that are tailored for a particular environment (such as dev, test, and production). You do not have to provide parameters in your template, but without parameters your template would always deploy the same resources with the same names, locations, and properties.