Update an Azure Resource Manager template

  1. Home
  2. Update an Azure Resource Manager template

Return to AZ-104 Tutorial

Here you will learn and understand how to update resources in an Azure resource manager template. On this page, you’ll understand the step by step process regarding the same. So, let’s begin.

There are various situations in which you require to update a resource during a deployment. So, there is a chance you may encounter this scenario when you are unable to define all the properties for a resource. 

For instance, if you create a backend pool for a load balancer, you might update the network interfaces on your VM to incorporate them in the backend pool. Certainly, Resource Manager maintains resources during deployment, still, you must design your template accurately to avoid errors. Furthermore, to ensure the deployment is operated as an update.

Therefore, first, you must reference the resource to create it. Later, then reference the resource by the same name to update it. And, in, case, two resources have the same name, Resource Manager delivers an exception. In order to avoid the error, define the updated resource in a second template that’s linked as a subtemplate utilising the Microsoft.Resources/deployments resource type.

Secondly, you must specify the name of the existing property to edit or a new name for a property to unite in the nested template. Also, you must designate the original properties and their original values. And, then if you fail to provide the original properties and values, Resource Manager thinks you wish to create a new resource and eliminates the original resource.

Example template

To give it a clear view, let’s first take a look at the resource object for our firstVNet resource. Notice that the settings must be defined again for our firstVNet in a nested template. Well, the reason being, Resource Manager doesn’t provide the same deployment name under the same template. And, nested templates are studied to be a different template. Further, specifying the values again for the firstSubnet resource. Then the Resource Manager must be updated in the existing resource rather deleting it and redeploying it. Lastly, the new settings for secondSubnet are picked up throughout this update.

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"resources": [
{
"apiVersion": "2016-03-30",
"name": "firstVNet",
"location":"[resourceGroup().location]",
"type": "Microsoft.Network/virtualNetworks",
"properties": {
"addressSpace":{"addressPrefixes": [
"10.0.0.0/22"
]},
"subnets":[
{
"name":"firstSubnet",
"properties":{
"addressPrefix":"10.0.0.0/24"
}
}
]
}
},
{
"apiVersion": "2015-06-15",
"type":"Microsoft.Network/networkInterfaces",
"name":"nic1",
"location":"[resourceGroup().location]",
"dependsOn": [
"firstVNet"
],
"properties": {
"ipConfigurations":[
{
"name":"ipconfig1",
"properties": {
"privateIPAllocationMethod":"Dynamic",
"subnet": {
"id": "[concat(resourceId('Microsoft.Network/virtualNetworks','firstVNet'),'/subnets/firstSubnet')]"
}
}
}
]
}
},
{
"apiVersion": "2015-01-01",
"type": "Microsoft.Resources/deployments",
"name": "updateVNet",
"dependsOn": [
"nic1"
],
"properties": {
"mode": "Incremental",
"parameters": {},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [
{
"apiVersion": "2016-03-30",
"name": "firstVNet",
"location":"[resourceGroup().location]",
"type": "Microsoft.Network/virtualNetworks",
"properties": {
"addressSpace": "[reference('firstVNet').addressSpace]",
"subnets":[
{
"name":"[reference('firstVNet').subnets[0].name]",
"properties":{
"addressPrefix":"[reference('firstVNet').subnets[0].properties.addressPrefix]"
}
},
{
"name":"secondSubnet",
"properties":{
"addressPrefix":"10.0.1.0/24"
}
}
]
}
}
],
"outputs": {}
}
}
}
],
"outputs": {}
}
Practice Test for AZ-104
Try the template

To deploy the template, run the subsequent Azure CLI commands:

az group create --location --name
az deployment group create -g \
--template-uri https://raw.githubusercontent.com/mspnp/template-examples/master/example1-update/deploy.json
  • Once deployment finishes, open the resource group you specified in the portal. 
  • Now, you’ll be able to see a virtual network named firstVNet and a NIC named nic1. 
  • After this, click firstVNet, then click subnets. You see the firstSubnet that was originally created, and you see the secondSubnet that was added in the updateVNet resource.
  • Now, go back to the resource group and click nic1 then click IP configurations. In the IP configurations section, the subnet is set to firstSubnet (10.0.0.0/24).
  • The original firstVNet has been updated rather being of re-created. If firstVNet had been re-created, nic1 would not be associated with firstVNet.
This image has an empty alt attribute; its file name is testpreptraining.com-2-750x117.png

Reference: Microsoft Documentation

Return to AZ-104 Tutorial

Menu