Deploying VM Using ARM Template

Azure Resource Manager (ARM) Templates are JSON files which define the resources which you need to deploy for your solution. ARM templates are community contributed and can be used by anyone from GitHub. This repository holds various predefined ARM templets to choose from as per your needs. But to get started with ARM templates, we will be configuring a simple virtual machine deployment from scratch so we can understand easily.

Prerequisites

  • Visual Studio 2015 or higher (any edition)
  • Azure account

Step 1

Run Visual Studio and log in to your Azure account before getting started.

Azure Resource Manager

Step 2

Got to file > New > Project to create a new project.

Azure Resource Manager

Step 3

In the new window, look for cloud and choose Azure Resource Group.

Give a name to the project and choose a location to be saved.

Click OK.

Azure Resource Manager

Step 4

In the new window, you can find various default templates and from the drop-down changing it to other options, you can browse even more templates from the internet. But for now, we will be choosing a blank template. Click OK.

Azure Resource Manager

Step 5

Go to solution explorer and you will find azuredeploy.json file. Double click to open. Now, you just see few blank {}. On the left side, you will find a JSON outline when you open azuredeploy.json. If not, you can go to view and open JSON outline.

Azure Resource Manager

Step 6

Under JSON outline, you can find

  • Parameters
    This is the parameters that you will be editing while deploying your template at step 10.

  • Variables
    This is going to be the variables associated with your resources. Eg: subnet range, vm size, storage account type, etc.

  • Resources
    This holds your different resources that you will be deploying. Eg: virtual machine, storage account, etc.

Output

This is going to hold the output data.

So, to add a resource, right-click on resources and choose "Add new resource".

Azure Resource Manager

Step 7

 To create a simple virtual machine we require four resources.

  • Virtual Network
    This is going to be the network in which your virtual machine resides. If you also want your storage account to be isolated you can bring it under this network by modifying the azuredeploy.json data.

  • Storage Account
    This is where your .vhd files reside which are used by the virtual machine.

  • Virtual machine
    The machine that you’re going to use virtually.

  • Public IP
    PIP gives you access to your virtual machine without PIP you won’t be able to access your virtual machine outside the virtual network.

  • NIC
    In addition to the above four resources, an additional resource will be created automatically which is called as Network Interface Card which is used to connect your virtual machine to the network.

If you deploy your template and check your Azure portal, this is the order in which your virtual machine will be created: first, the virtual network, second storage account, third NIC, then virtual machine, and finally, your PIP. Let’s maintain the same order and add our resources by redoing step 6 until we add all resources.


Azure Resource Manager

Azure Resource Manager

While creating the virtual machine we have to choose the virtual network we wish to use. By default this templates creates a virtual network with an address range of xxx.xxx.xxx.xxx/16 and it is subdivided into two equal subnets with the address range of xxx.xxx.xxx.xxx/24 for both. If you don’t require this many address ranges you can edit your Virtual network address range and remove a subnet. Note: for a virtual network to work you need at least one subnet address. Without a subnet address, you cannot add any resources to it.

Azure Resource Manager

Here, you can see on the JSON outline panel. After adding the virtual machine, NIC gets created automatically.

Azure Resource Manager

Step 8

After creating the resources, head back to solution explorer and right click your project name and choose Deploy > New.

Azure Resource Manager

Step 9

If you haven’t logged in before you have to log in to your Azure account to proceed further. Once logged in you will have your subscription drop down. If you have multiple subscriptions you can choose under which one you want to deploy your template.

Then, you have to create a new resource group if you don’t have any or choose existing one if you have. While creating the resource group you have to choose a location, which is going to be the location of the data center in which you want to deploy your resources.

And, click Create.

Azure Resource Manager

Azure Resource Manager

Step 10

Once the resource group has been created click on edit parameters button and here you need to enter the virtual machine name, username, password, OS type, storage account type, and DNS name. Remember, the DNS name should always be in lowercase. You can add a number to it but no special characters. And it should be a unique name. If not, the deployment will fail with an error DNS name already exists. Choose save password as plain text if you want and click save then deploy.

Azure Resource Manager

Azure Resource Manager

Step 11

This step is optional. If you deploy and you get an error “object reference not set to an instance of an object” and a visual studio market place shows up with the inbuilt browser it could indicate two causes. Either a newer version of PowerShell is not installed or your Azure sdk is out of date. In my case it was both. So I had to install PowerShell and go back to visual studio installer and update azure sdk.

Azure Resource Manager

Azure Resource Manager

Step 12

Once it is updated, redeploy from step 9 by right clicking on Project Name >Deploy and under New, you can find the resource group you created already and can choose that and deploy. Once the deployment is successful you will get an output something like the below screenshot. Now you can head back to the Azure portal and check your resource group folder to see your resources and from there, you can choose your virtual machine and download RDP to log into your machine.

Azure Resource Manager

Azure Resource Manager