Understanding Nested ARM Templates With An Example

This article demonstrates the concept of Nested ARM Templates and their benefits with an example.

The Problem Context

While creating resources using ARM templates, we generally used to create a single JSON template that can be used to deploy all these resources.

But if we want to increase the maintainability of our templates, because these templates can grow quite large over the period. Also, it could become difficult if we have too many resources defined in that one template.

So to simply resolve this we can create separate templates for each resource. For example, we want to create a VM and storage account and a web app, just we have one main template that could go ahead and call each one of these templates as linked templates.

It makes it much easier to maintain our templates because as you know the creation of the VM with the help of a template, there is already a lot of resources that get deployed in that template because they are required for the VM. A similar example could be the Azure SQL database.

When we want to go ahead and deploy a big set of resources, we can break them down into individual templates.

Understanding Nested ARM Templates With An Example

A nested template is just basically one template within another. The advantage of a nested template is that you can go ahead and deploy resources across resource groups.

Building a Nested ARM Templates

Let’s see an example of a nested template.

This template will be used to deploy a storage account into one resource group and then our main template is going to deploy another storage account into a different resource group. Here, I have a nested template with parameters called the inner resource group.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "innerResourceGroup": {
      "type": "string"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "stprimary2021",
      "location": "[resourceGroup().location]",
      "sku":{
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {
      }
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2019-10-01",
      "name": "nestedTemplate",
      "resourceGroup": "[parameters('innerResourceGroup')]",
      "properties": {
      "mode": "Incremental",
      "template": {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {},
          "variables": {},
          "resources": [
          {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "name": "stsecondary2021",
            "location": "[resourceGroup().location]",
            "sku":{
              "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {
            }
          }
          ]
      },
      "parameters": {}
      }
    }
  ]
}

Here, we can see that the nested template is part of resource Microsoft.Resources/deployments. And first storage account “stprimary2021” will create on resources group “rg-myapp-eastus” in the eastern US region. Similarly, the second storage account “stsecondary2021” will create on resources group “rg-myapps-centralus” in the central US region.

Deploying Nested Templates into Azure

Let's go ahead and deploy the template from azure. Create a template deployment and paste this template and choose the resource group and then review + create.

Understanding Nested ARM Templates With An Example

Understanding Nested ARM Templates With An Example

Once the template deployment is complete, let’s check storage accounts.

Understanding Nested ARM Templates With An Example

We can see how easily we created a nested template with an example.

Happy Learning!