Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should we use a parameter when to use variable in ARM templates

I am confused about where to use a variable and where to use a parameter in ARM templates. How do we make this call ?

The referenced script uses both. I am more curious of the justification of using variables.

enter image description here

Reference

Sample Service Fabric Azure Deploy Script

https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/service-fabric-oms/azuredeploy.json

like image 800
Rıfat Erdem Sahin Avatar asked Jul 03 '17 13:07

Rıfat Erdem Sahin


People also ask

What are parameters in ARM templates?

Parameters allow you to pass different values to the ARM template for use during the deployment. Some common examples include names of resources or which Azure region to host them. Parameters enable your templates to be more dynamic and used across different environments.

What is variable and parameter in Azure?

Parameters are external values passed into pipelines. They can't be changed inside a pipeline. Variables, on the other hand, are internal values that live inside a pipeline. They can be changed inside that pipeline.

What is the maximum number of parameters you can use in an ARM template?

You're limited to 256 parameters in a template. You can reduce the number of parameters by using objects that contain multiple properties. Name of the parameter.

How do you make an ARM template parameter optional?

The default value attribute is used to make a parameter optional: if you deploy the template and don't specify a value for a particular parameter, ARM will just use the default value configured on that parameter. If you don't specify a value and the parameter doesn't have a default value, you get a deployment error.


2 Answers

In the Azure template json file:

parameters:Values that are provided when deployment is executed to customize resource deployment.

variables:Values that are used as JSON fragments in the template to simplify template language expressions.

More information please refer to this official document:Understand the structure and syntax of Azure Resource Manager templates.

I am more curious of the justification of using variables.

Based on my experience, if you only use the variable once, you don't need use variables. But if you want to use the variable multiple times, you had better use variables. Using variable can simplify your template to avoid duplication of content.

For example, if you don't use supportLogStorageAccountName more than once, you can just do:

"name": "[toLower(concat('sf', uniqueString(resourceGroup().id),'2'))]"

However if you use provide variable supportLogStorageAccountName several\many times, you can use variable to avoid duplication.

like image 195
Shui shengbao Avatar answered Oct 15 '22 05:10

Shui shengbao


ARM templates are usually used to create a set of close to identical environments. The parameters are what differs them. This is commonly used for environment type (prod, dev, test) and performance/cost related parameters. Variables are used to create unique names for services based on or calculated from the parameters.

An example of this would the name of a storage account. This is usually done by concatenating a common name like _storage and an environment name parameter like “test” and store it in a variable. When you create another environment all you must do is change the environment type parameter.

like image 40
Espen Avatar answered Oct 15 '22 03:10

Espen