Step by Step ARM Templates - Authoring ARM Templates using Visual Studio
@20aman Sep 17, 2016Index 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.
- To get started just launch the Visual Studio from the Start menu.
- Next, Create a new Project of type "Azure Resource Group" by navigating to Templates -> Visual C# -> Cloud
-
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.
-
Project is created with various folders and files. You can explore the project in the solution explorer in Visual Studio.
Let us see what these folders and files are:- 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.
- 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.
- Tools: This folder contains the "AzCopy.exe" file to help you copy any artifacts to Azure.
-
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:- parameters
- variables
- resources
-
Next, let us look at JSON Outline panel and check how it can provide us more information and help us in authoring templates.
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:- StorageAccount
- PublicIPAddress
- VirtualNetwork
- NetworkInterface
- VirtualMachine
Adding New Resource
Let's assume you want to add a new resource to this template. You have 2 ways to achieve the same:- Method 1 - Create a new resource by modifying and adding the JSON for the new resource in the template.
- 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.
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.
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.