Using Parameters in ARM Templates
What is Parameter
In ARM Template parameter is the value that is provider which cannot be computed or worked out by azure (eg names of resources)
Basic Parameter Syntax
"parameters": {
"<parameter-name>" : {
"type" : "<type-of-parameter-value>",
"defaultValue": "<default-value-of-parameter>",
"allowedValues": [ "<array-of-allowed-values>" ],
"minValue": <minimum-value-for-int>,
"maxValue": <maximum-value-for-int>,
"minLength": <minimum-length-for-string-or-array>,
"maxLength": <maximum-length-for-string-or-array-parameters>,
"metadata": {
"description": "<description-of-the parameter>"
}
}
}
Using Parameters
- To use the Parameters we would use the same example referred in creating basic arm template which is here
- To Start with we have the following structure of arm template
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [{
"name": "myVirtualNetwork",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2018-08-01",
"location": "East US",
"tags": {
"From": "ARM Template",
"Reason" : "Learning"
},
"properties":{
"addressSpace": {
"addressPrefixes":[ "10.1.0.0/16"]
},
"subnets":[
{
"name": "myVirtualSubnet",
"properties": {
"addressPrefix": "10.1.0.0/24"
}
}
]
}
}]
}
- In the above mentioned examples i would like to parametrize the names
- virtual network name
- subnet name
- From the syntax section if i have to write the basic structure for virtual network name. It would be like
"networkName":{ "type": "string", "defaultValue": "myVirtualNetwork", "metadata": { "description": "virtual networks name" } }
- similarly the subnet name can be written as
"subnetName":{ "type": "string", "defaultValue": "myVirtualSubnet", "metadata": { "description": "Subnet's name" } }
- add the above two steps snippets to your template
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"networkName":{
"type": "string",
"defaultValue": "myVirtualNetwork",
"metadata": {
"description": "virtual networks name"
}
},
"subnetName":{
"type": "string",
"defaultValue": "myVirtualSubnet",
"metadata": {
"description": "Subnet's name"
}
}
},
"resources": [{
"name": "myVirtualNetwork",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2018-08-01",
"location": "East US",
"tags": {
"From": "ARM Template",
"Reason" : "Learning"
},
"properties":{
"addressSpace": {
"addressPrefixes":[ "10.1.0.0/16"]
},
"subnets":[
{
"name": "myVirtualSubnet",
"properties": {
"addressPrefix": "10.1.0.0/24"
}
}
]
}
}]
}
- Now in the resources section we need to remove the hardcoded names of network & subnet with parameters. For that we need to use specific function called parameters as describe over here script would look as
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"networkName":{
"type": "string",
"defaultValue": "myVirtualNetwork",
"metadata": {
"description": "virtual networks name"
}
},
"subnetName":{
"type": "string",
"defaultValue": "myVirtualSubnet",
"metadata": {
"description": "Subnet's name"
}
}
},
"resources": [{
"name": "[parameters('networkName')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2018-08-01",
"location": "East US",
"tags": {
"From": "ARM Template",
"Reason" : "Learning"
},
"properties":{
"addressSpace": {
"addressPrefixes":[ "10.1.0.0/16"]
},
"subnets":[
{
"name": "[parameters('subnetName')]",
"properties": {
"addressPrefix": "10.1.0.0/24"
}
}
]
}
}]
}
- In the same manner i would like to parametrize the address space array for the network & address space string for subnet. The script will be change to as shown below
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"networkName":{
"type": "string",
"defaultValue": "myVirtualNetwork",
"metadata": {
"description": "virtual networks name"
}
},
"subnetName":{
"type": "string",
"defaultValue": "myVirtualSubnet",
"metadata": {
"description": "Subnet's name"
}
},
"networkAddressSpaces":{
"type": "array",
"defaultValue": [ "10.1.0.0/16"]
},
"subnetAddressSpace":{
"type": "string",
"defaultValue": "10.1.0.0/24"
}
},
"resources": [{
"name": "[parameters('networkName')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2018-08-01",
"location": "East US"y,
"tags": {
"From": "ARM Template",
"Reason" : "Learning"
},
"properties":{
"addressSpace": {
"addressPrefixes":"[parameters('networkAddressSpaces')]"
},
"subnets":[
{
"name": "[parameters('subnetName')]",
"properties": {
"addressPrefix": "[parameters('subnetAddressSpace')]"
}
}
]
}
}]
}
- Lets execute this deployment. I would be using CLI to create this deployment. Refer here
az group create --name "testing" --location "East US"
az group deployment create --resource-group testing --template-file .\myfirstarm.json --name myfirstparameterdeployment
- Since we have added parameters, lets change the values of parameters during deployment execution
- Lets create a file which consists of all the values of parameters to be passed. I have decided to pass the following
Name: myNewNetwork. (type = string)
Address space: 192.168.0.0/16. (type = array)
Subnet - Name : myNewSubnet. (type = string)
Subnet - Address range: 192.168.0.0/24. (type = string )
- For this i would build a json file containing all of my parameters. I would need to consider types as well Basic structure of the parameters file is
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"<parameter-name>": {
"value": <parameter-value>
}
}
}
- If we would create parameters file with name parameters.json for our purpose it would look like
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"networkName": {
"value": "myNewNetwork"
},
"subnetName": {
"value":"myNewSubnet"
},
"networkAddressSpaces": {
"value":["192.168.0.0/16"]
},
"subnetAddressSpace": {
"value":"192.168.0.0/24"
}
}
}
Lets execute this deployment with parameters file . I would be using CLI to create this deployment. Refer here
az group create --name "testing" --location "East US"
az group deployment create --resource-group testing --template-file .\myfirstarm.json --name myfirstparameterdeployment --parameters "@parameters.json"
Very use full stuff for beginners !!