Azure Resource Manager Linked Templates Using Visual Studio

In one of our previous examples listed below, we have learned how to create simple resources using ARM Templates.

Below are the two disadvantages in the approach taken in both of the articles listed above.

  1. Both of the ARM templates are very simple. In real time, we might need to create a complex infrastructure where we might have to create multiple VMs, IPAddresses, NIC cards, Databases, Load Balancing etc.

  2. No reusability of the resources within the template. For example, we created both (1) App Service Plan and (2) App Service Web App in the same template file. If you want to create another App Service Web App for another requirement, we would have to copy the code and re-use the same in the next ARM template.

  3. There is no facility of multiple developers working on creating a complex infrastructure using the ARM template.

Today, we will be discussing how to overcome all of the above concerns / disadvantages using one of the cool features of the Azure Resource Manager (ARM) templates named “Linked Templates”

Yes, you can link a Master template with one or more child templates and pass inputs to the child templates and get back the output values from the child parameters much like functions with return values in any of the programing languages or like User Controls and Partial View in ASP.NET and MVC respectively.

Let’s start authoring the ARM Template file.

Create a new project and select “Cloud” - “Azure Resource Group” as shown below.

new

In the next window select “Blank Template” and click on “OK” as shown below.

Blank Template

Below is how the Master ARM template (azuredeploy.json) looks as soon as you click on “OK” button.

Navigate to Azure portal and create a Resource Group something like “LinkedTemplateResourceGroup” as shown below.

LinkedTemplateResourceGroup
Now, create a new Storage Account something like “linkedtemplatesa” in Azure. We will need to store the Artifacts in the Storage Account.

create
Open the JSONOutline window in Visual Studio and create a new resource by right clicking on “resources(0)” and selecting “Add New Resource” as shown below.

resources

In the “Add Resource” window, select “Nested Deployment” to create a linked template as shown below.

Add

Once you click on “Add” in the above screen capture, the corresponding “Infrastructure as Code” will be created as shown below.

Add

The important point to note here is the highlighted text in the above screen capture “Microsoft.Resources/deployments”. Yes, “deployments” is the name of the resource.

Also, Visual Studio has created two more files as shown below.

files

  • Linkedtemplate.json This is the actual linked template. We can also call it the child ARM template.

  • Linkedtemplatefile.paramters.json This is the parameter file of the child ARM template. In this article we are not going to use this.
Now, open the new Child ARM template that we have just created and created the following two new resources.
  • An App Service Plan with the named “LinkedTemplateAppPlan”,

    • After you create the “App Service Plan” the required code will be created. Now, add the code “"defaultValue": "LinkedTemplateAppPlan" in the parameters section and a “comma” before that as shown below. (For now, we are providing the name of the App Service plan as a default value instead of providing it from Properties file.)

      code

  • An App Service Web App named “LinkedTemplteWebApp”,

    • Now, create a new Web App as shown below.

      Web App

      • Visual Studio will create the following code in the Child Template file.

        code

So, here are two ARM template files along with the following resources.

  • Master Template  This template has one resource named “deployments”.

  • Child Template This template has two resources (1) App Service Plan and (2) App Service Web App.

We are done with authoring the Linked Template files (link between Master Template and the Child Template). The most important tag here is the “templateLink” node of the Master template which is shown below.

code

Let’s deploy the ARM Template to Azure Resource Group by right clicking on the “Project” as shown below and click on “New Deployment”,

New Deployment

Clicking on the “New Deployment” in the above step will open up the following window.

window

Below are the details of the above window

  • Subscription Your subscription.

  • Resource Group Select the resource group which you have created initially.

  • Deployment template This is the Master Template.

  • Template parameters file this is the “.parameters.json” file of the Master Template.

  • Artifact Storage Account

    Select the storage account that you have created initially. This is required for storing the artifacts. Basically, Azure Resource Manager cannot execute the commands located in our Development environment. They should be located in a location which is accessible by Azure. So, In this article we will store them in Azure Storage account. Please note that we don’t need to upload these artifacts to Azure’s storage account manually. Clicking on the “Deploy” button will automatically store them in Azure blobs. We will see the screen capture of the same later.

Let’s go and click on “Deploy” button. That’s it, we have created resources using Linked Templates as shown below.

Deploy

Deploy
Let’s navigate to the storage account “linkedtemplatesa” of the above screen capture by clicking on the storage account.

linkedtemplatesa

Now, click on the “Blobs”. Azure will display all the Containers within the selected blob as shown below. For now, we have only one container.

Blobs

Now, click on the container. You will see the Folder named “LinkedTemplate which contains all the files.

LinkedTemplate

Finally, click on the folder, you will see the sub folders.

folder
Finally by clicking on the sub-folders, you will see the templates file.

Hope you enjoyed the article. I appreciate any suggestions for improvement.

Up Next
    Ebook Download
    View all
    Learn
    View all