Step by Step ARM Templates - What is in an ARM Template - Understanding Components 2 - Parameters
@20aman Aug 24, 2016Index of all blogs in this Step by Step ARM Templates series is located here: Step by Step Azure Resource Manager (ARM) Templates - Index
If you haven't checked the previous blog on the overall structure of the ARM template, I suggest you give it a quick read before checking the component described in this post in detail.
Parameters
As mentioned earlier, parameters are the way to customize the templates, each time you deploy it to create resources in Azure. These parameters are the end-user inputs for various aspects of the template. E.g. If you are deploying an Azure Virtual Machine via an ARM Template then the name of the VM can be an input parameter. Operating System type can be another parameter.
The parameters can be referred and used in other parts of the ARM Template.
1. Defining Parameters
Parameters is a one huge JSON object with multiple JSON properties. Each property is one parameter which is represented as another JSON object. Let us look at its structure at a high level.
"parameters": {
"parameter 1" : {},
"parameter 2" : {},
"parameter 3" : {}
}
E.g. If you are creating a template to deploy a Windows Virtual Machine then the parameters will look something like below:
"parameters": {
"VMName" : {},
"AdminUserName" : {},
"AdminPassword" : {},
"WindowsOSVersion" : {}
}
Now let us look at one of the parameters. E.g. The AdminUserName parameter will look like:
"adminUsername": {
"type": "string",
"minLength": 1,
"metadata": {
"description": "Username for the Virtual Machine."
}
}
The parameter object, as shown above, has following parts:
- Type - This is the data Type of the parameter.
- minLength - This is the minimum length the parameter must have
- Metadata - This is just to provide a description as to what the parameter means.
The Data Type allowed for the parameter are:
- string or secureString – any valid JSON string
- int – any valid JSON integer
- bool – any valid JSON boolean
- object – any valid JSON object
- array – any valid JSON array
A more complex parameter e.g. Windows OS Version, with few more properties, is shown below:
"windowsOSVersion": {
"type": "string",
"defaultValue": "2012-R2-Datacenter",
"allowedValues": [
"2008-R2-SP1",
"2012-Datacenter",
"2012-R2-Datacenter"
],
"metadata": {
"description": "The Windows version for the VM. This will pick a fully patched image of this given Windows version. Allowed values: 2008-R2-SP1, 2012-Datacenter, 2012-R2-Datacenter."
}
}
It has additional below properties:
- Default Value - This is the default value. End User will be able to change this value when deploying the template. If no value is provided then this value is picked.
- Allowed Values - This is an Array of values which are allowed for the parameter. Only value from this set is allowed as an input.
2. Using Parameters
Using parameters is easy. Wherever in your template (in variables or resources section) you want to use the value of a parameter, just use the parameter function as shown below with the name of the parameter as input, enclosed in square brackets.
[parameters('windowsOSVersion')]
If the parameter value is assigned to a property, enclosing it in double quotes, as shown below:
"sku": "[parameters('windowsOSVersion')]"
3. Best Practices
- Try to always provide Default Values
- Provide metadata so that you can provide insight as to what the parameter is meant for
- Provide complete descriptive names, no matter how long.
- Use Pascal Casing to name your parameters. i.e. First letter should be a small letter. Then every new word will have the first letter as a capital. No space between words. E.g. windowsOSVersion
- Use properties like minLength and Allowed values to impose restrictions. This reduces any human errors.
That's all there is to Parameters in ARM Templates. If you have any doubts, please comment in the below section. Use the links at the Top to know all about other components in an ARM Template.