Step by Step ARM Templates - Authoring ARM Templates using Visual Studio

@20aman    Sep 17, 2016

Index of all blogs in this Step by Step ARM Templates series is located here: Step by Step Azure Resource Manager (ARM) Templates - Index

Visual Studio is a very powerful tool when it comes to authoring ARM Templates.

Key features which make it a tool of our choice are:

  • In-house support for ARM Templates
  • Smart IntelliSense
  • Pre-populated templates for various Azure resources
  • JSON Outlining
  • Easy Deployment options

The screenshots in this blog post are from Visual Studio 2013. You can use other newer versions as well.

Pre-Requisites

You need to have Azure SDK installed to get true power of Visual Studio with Azure integration. If you don't have it already, you can install the same from here: Azure SDK Downloads

Authoring First ARM Template in Visual Studio

Authoring with Visual Studio is very easy.

  1. To get started just launch the Visual Studio from the Start menu.
  2. Next, Create a new Project of type "Azure Resource Group" by navigating to Templates -> Visual C# -> Cloud

    New Project
  3. Next, you will be presented with a dialog to "Select Azure Template". If you want to author from scratch then choose a Blank Template. Else select one of the starter template. For this blog, we will be using "Windows Virtual Machine" Template.

    Selecting Azure Template
  4. Project is created with various folders and files. You can explore the project in the solution explorer in Visual Studio.

    Solution Explorer
    Let us see what these folders and files are:
    1. Scripts: The single PS1 file is to create a new Resource Group and deploy the ARM Template. It uses "New-AzureRmResourceGroupDeployment" PowerShell cmdlet to deploy the template.
    2. Templates: "WindowsVirtualMachine.json" is the main ARM Template file that we are interested in. Also, "WindowsVirtualMachine.parameters.json" is the parameters file for the ARM template.
    3. Tools: This folder contains the "AzCopy.exe" file to help you copy any artifacts to Azure.
  5. Double click and open the "WindowsVirtualMachine.json" file to open it. You will be presented with a huge JSON file. Collapse the section by clicking the small "-" signs to the left of the file. Also notice the JSON Outline panel to the left. This is your biggest friend in Visual Studio when authoring ARM Templates.


    You can immediately notice that key sections both in the template in the middle and in the JSON Oultine panel on the left (in the image above) are:
    1. parameters
    2. variables
    3. resources
    You can click on any of the elements in the left JSON Outline panel and the same section will be highlighted in the center, in the JSON template file.
  6. Next, let us look at JSON Outline panel and check how it can provide us more information and help us in authoring templates.

    JSON Outline Panel
    You can see that the panel provides a special icon for each type of the resource. In our current template the various resources listed are:
    1. StorageAccount
    2. PublicIPAddress
    3. VirtualNetwork
    4. NetworkInterface
    5. VirtualMachine
    Click on each of the resources and inspect how their JSON structure looks and differs. You will immediately notice that the major difference in each of these resources is in their Type and Properties.

    Adding New Resource

    Let's assume you want to add a new resource to this template. You have 2 ways to achieve the same:
    1. Method 1 - Create a new resource by modifying and adding the JSON for the new resource in the template.
    2. Method 2 - Let Visual Studio add the resource for you. Right click anywhere in the resources area of the JSON Outline Panel or the small "+" box at the top left of the panel (as shown in the image above) and VS will give you a new popup to add the resource from pre-defined resources as shown below.
    Add Resource
    Once you click Add the JSON for resource will be added to the template and the corresponding new element will appear in the JSON Outline Panel.

    Deleting a Resource


    If you need to delete a resource, simply right click on that resource in the JSON outline panel on the left and then select "Delete Resource".

    Using Intellisense

    The last thing to notice is the use of Intellisense in Visual Studio which helps you as you are editing the templates.

    Intellisense
    When you type quotes the closing quotes are automatically provided. Also, as you can see in the above image the various valid values, that can come there are also shown along with small tooltip about the data type. If the Intellisense doesn't come up automatically, then press Ctrl + Space to get Intellisense.

In the end, the Visual Studio makes authoring ARM templates much more manageable and easy for you.

In the next blog, we will see how to use Visual Studio to Deploy the templates.





Comments powered by Disqus