Creating VMs, VNET, Subnets, And Load Balancers Using Azure Resource Manager Templates

Using the ARM Template Deployment using PowerShell, we are going to deploy 2 Virtual Machines under a Load Balancer and configures Load Balancing rules for the VMs. Here in this demo, I have used the template available on the link here:

 
 https://github.com/Azure/azure-quickstart-templates/tree/master/201-2-vms-loadbalancer-lbrules 
  • This template allows you to create 2 Virtual Machines under a Load Balancer and configure a Load Balancing rule on Port 80. This template also deploys a Storage Account, a Virtual Network, Public IP Addresses, Availability Set and Network Interfaces.
  • In this template, we use the resource loops capability to create the Network Interfaces and Virtual Machines.
  • You can get more such templates from Azure's GitHub from the link here: - https://github.com/Azure/azure-quickstart-templates  
  • So now let us go ahead with the demo. We will have two files. One will be the azuredeploy.json file and the other one will be azuredeploy.parameters.json file.
  • The parameters file will pass certain runtime parameters to the azuredeploy.json file.
Here, I have got both of my files from GitHub. Next, after this, we need to check if these files have valid JSON code or not. So, we need to lint these two files.
 
azure
 
You can go to www.jsonlint.com to check if your JSON code is valid or not. Here I have checked mine and I am good to go.
 
azure azure 
 
Next, you need to login to your Azure account using PowerShell.
 
azure 
 
After that, we need to create a Resource Group. Run this command to create a new Resource Group and it will get created instantly.
 
azure 
 
Now, it is time to check if our template and the parameters file are good to deploy or not.
Run this command to see if the template and the parameters files are okay. It is okay, it will return nothing. No result is a good result.
 
azure
 
If I add something to my main azuredeploy.json file that is not in the parameters file, let us see what happens.
 
azure 
 
Then if I run the test command over my template it tells me that the template is invalid. Undo the changes in the template file. Now, let us make some changes to the Parameters.json file.
 
azure
 
Right now it looks something like this.
 
azure 
 
I have made a slight change to the value of dnsName. Let us see what happens next.
 
azure 
 
It tells me that it is an InvalidTemplateDeployment. Now apparently, you cannot actually figure out what the error is from PowerShell. It just gives you some tracking ID. But I know that the values in the parameters file accepts only small characters and not the capital ones.
 
azure
 
But there is a way where you can debug your command and see what the error is. Just add -debug at the end of your test command.
 
azure
 
You will be able to find the error down there. So undo the name changes in the parameters file now.
 
azure 
 
And it should run fine again. But, keep in mind that this doesn't mean that there won't be any runtime errors. There might be those as well. But as of now, we are good to go.
 
Now, running this command will start the deployment of the template. The PowerShell will remain blank for a while until everything gets deployed. You can close PowerShell and it will still continue the deployment but you will not get the report of deployment.
 
azure 
 
You can see that it has deployed all of the resources except my VMs. The reason behind that is that the VM sizes mentioned are not available in the region I have selected.
 
azure
 
This is what the runtime error looks like.
 
azure 
 
In the portal under the Deployments blade of my resource, it shows me a failure.
 
azure
 
Since the two VMs didn't get deployed, it is showing me failed.
 
azure 
 
In my azuredeploy.json file, I have changed the value of vmSize to Standard_D2_v2. Now it shouldn't show me any errors. So now I will redeploy it with another name i.e. KTDemoDeploy2.
 
azure 
 
Inside the Deployment Tab, you can see that it is now deploying our resources. You can delete the older deployment since it had failed to deploy successfully.
 
azure
 
It has deployed almost everything but it is still showing that it is deploying. That is because it is doing some backend configuration like connecting the NICs with VMs and other such stuff.
 
azure
 
In the portal, it now shows succeed.
 
azure
 
And in PowerShell as well, it shows succeed.
 
azure 
 
Hence, this is how you can deploy the Azure Resource Manager Templates.