Home » Azure » Building your first Azure Resource Group template for IaaS

Building your first Azure Resource Group template for IaaS

At the BUILD conference last week Microsoft announced the public preview for template-based deployments of Compute, Networking and Storage, using the Azure Resource Manager. With this new template feature we can build complex models of services and deploy in an easy way. If you have been building large PowerShell scripts before for deployment you should take a look at this, as it is easier to work with. In this blog post I want to show you how to get started building a resource manager template for a new virtual machine.

To build the template I am using Visual Studio. If you are an IT pro like me everything with Visual Studio is often a bit scary, but in this case it is really easy J On top of Visual Studio you need to install latest version of Azure SDK and latest Visual Studio updates. I installed both the Azure SDK and Visual Studio with default settings.

  1. Start Visual Studio and select to create a new project
  2. Select to create a Azure Resource Group project
  3. Name the new project, for example Contoso Single Server Template, click OK
  4. On the Select Azure Template page, select Windows Virtual Machine and click OK. This template will deploy a Windows VM with a couple of different options. It is a good foundation to then add on more to.
  5. Visual Studio have now generated a three files for you, and this is all you need to start. You now have a working template that you can start using direct. There is a configuration file (WindowsVirtualMachine.json) that includes all the settings and details of what you want to deploy to Azure Resource Manager, and there is a parameter file (WindowsVirtualMachine.param.dev.json) that includes all user defined values that he configuration file needs. There is also a PowerShell script (Deploy-AzureResourceGroup.ps1) that is used by Visual Studio to deploy you template. You can also see AzCopy.exe in the Tools folder. AzCopy is used by PowerShell to copy files to a storage account container, if you template includes files and custom code to deploy.
  6. You can now test the template by right-click on Contoso Single Server Template and select Deploy, New Deployment. Connect to your Azure subscription and configure all parameters. Then click Deploy.

  7. You can now see that the deployment has started in the Output window in Visual Studio. You can also log on the Azure Preview portal and follow the deployment in your new resource group. Once the deployment is done, you can see the result in the Output window in Visual Studio.

    We have now deployed one instance of the new resource group template, including a virtual machine. Let’s say we want to add one more virtual machine to the template.

  8. In Visual Studio, Solution Explorer, select WindowsVirtualMachine.json and then click on the JSON Outline tab
  9. To add a virtual machine, right-click resources and select Add Resource
  10. In the Add Resource wizard, select Windows Virtual Machine. Input VM02 as Name and select the already existing storage account and virtual network. Click Add
  11. If you look in the JSON Outline window, at parameters, you can see that Visual Studio just added a number of parameters for VM02
  12. To update the already existing deployment with the new virtual machine, right-click Contoso Single Server Template and select Deploy
  13. Fill in the parameters for VM02 and then click Deploy
  14. You can watch the deployment of the second VM both from Visual Studio and the Azure Preview Portal.

We have now built a template that deploys two virtual machines. As you notice when we did the second deployment we needed to input VM admin user name and password twice. Let look at how we can use the same parameter for both virtual machines

  1. In Visual Studio, in Solution Explorer, click the WindowsVirtualMachine.json file
  2. In JSON Outline tab, click VM02, and you will see the code for VM02 is highlighted, scroll to osProfile
  3. As we want to use the same parameter for admin credentials on VM02 as on the first VM replace the VM02 parameters with the parameter we use for the first VM
  4. Delete VM02AdminUserName and VM02AdminPassword parameters in the JSON Outline tab
  5. In Visual Studio, in Solution Explorer, click the WindowsVirtualMachine.param.dev.json file
  6. In the param.dev.json file, remove the VM02AdminUserName parameter
  7. Now, do a new deployment and verify you can connect to both new virtual machines with the admin credentials

When you are done with your template you can right-click Contoso Single Server Template and choose Build (same place as Deploy). This will build your solution and you can now copy it from the project folder, default folder C:\Users\<username>\Documents\Visual Studio 2013\Projects\Contoso Single Server Template\Contoso Single Server Template. In the Scripts folder you will find the PowerShell script that you can use to deploy the template. In the Templates folder is the two JSON files. To deploy an instance of the template you can run the PowerShell script from Azure PowerShell.

Another way to deploy with the template is to paste the template code into Template Deployment in the Azure Portal. The third way to deploy using the template is to upload it to a storage account and then trigger it direct into the Template Deployment feature. A tricky part with that is that you have to replace escape all the special characters, for example

https://contoso11.blob.core.windows.net/scripts/WindowsVirtualMachine.json

Becomes

https://ms.portal.azure.com/#create/Microsoft.Template/uri/http%3A%2F%2Fcontoso11.blob.core.windows.net%2Fscripts%2FWindowsVirtualMachine.json

In this blogpost we have discussed how we can use Visual Studio to build resource group templates for IaaS resources. We can then deploy resources with the template from both the Azure Portal and from Azure PowerShell.


5 Comments

  1. Great, Thanks for the article, I have created the template and stored in the D drive,When i tried to deploy through powershell cmdlet

    New-AzureRmResourceGroupDeployment -Name technodply -ResourceGroupName technophilrsgrp -TemplateFile -TemplateParameterFile <D:\Visual-Studio-Lab

    \AzureResourceGroup1\AzureResourceGroup1\Template\WindowsVirtualMachine.parameters.json "\AzureResourceGroup1\AzureResourceGroup1\Template\WindowsVirtualMachine.parameters.json Following error, can you pls help
    New-AzureRmResourceGroupDeployment : Access to the path 'D:\Visual-Studio-Lab' is denied."

  2. This is one great post, I am going to recreate a VM using exisiting VHD since Azure Recovery services vaults are not yet fully functional. I am definitely going to use information from this post, thank you

  3. Great article! No response yet from others, but, this is becoming THE way to do things in Azure. Thanks Anders

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.