How do I use a template to create VMs in Azure IaaS v2 (ARM)?

Create VMs using templates in IaaS v2.

John Savill

June 24, 2015

3 Min Read
How do I use a template to create VMs in Azure IaaS v2 (ARM)?

Q. How do I use a template to create VMs in Azure IaaS v2 (ARM)?

A. Using a JSON template provides a repeatable and resumable way to create one VM or multiple VMs which can even make up a multi-tiered services. Services inside a template are deployed in parallel unless there are dependencies. The resource at the bottom of the dependency stack is deployed first and then the resources that depend on that resource and so on.

While you can create your own templates using any JSON editor, including a regular text editor or more advanced tools such as Visual Studio, the best way is to look at existing templates which are available from GitHub. The GitHub template resource (https://github.com/Azure/azure-quickstart-templates) is the library for Azure Resource Manager (ARM) templates and a better formatted version is available at http://azure.microsoft.com/en-us/documentation/templates/. A great template to start with is the 101-simple-windows-vm which deploys a single VM. Notice there are a number of files for all templates:

  • .json - This is the main template file

  • .parameters.json - The parameters that can be passed to the template and while optional provides a way to prepopulate values that can be passed when creating an instance of the template

  • metadata.json - Data about the template used by Azure to enable its use and publication

  • README.md - Information about the template and often a button that deploys the template to Azure by loading the template into the template editor in the Azure portal

Each file can be viewed in the GitHub by selecting the file and downloading. The only file you must download is the main template file. There are also templates in Azure can also be viewed and downloaded using PowerShell once you have authenticated with an Azure AD account and switched to ARM mode:

  • Add-AzureAccount

  • Switch-AzureMode -Name AzureResourceManager

Templates can then be viewed and downloaded.

Get-AzureResourceGroupGalleryTemplate -Publisher MicrosoftGet-AzureResourceGroupGalleryTemplate -Identity Microsoft.WindowsServer2012Datacenter.0.2.77-previewSave-AzureResourceGroupGalleryTemplate -Identity Microsoft.WindowsServer2012Datacenter.0.2.77-preview -Path D:TempMicrosoft.WindowsServer2012Datacenter.0.2.77-preview.json

Open up the downloaded JSON file and view the parameters that are required. Below is an example parameter section:

"parameters": {    "newStorageAccountName": {  "type": "String"    },    "newDomainName": {  "type": "String"    },    "newVirtualNetworkName": {  "type": "String"    },    "vnetAddressSpace": {  "type": "String"    },    "hostName": {  "type": "String"    },    "userName": {  "type": "String"    },    "password": {  "type": "SecureString"    },    "location": {  "type": "String"    },    "hardwareSize": {  "type": "String"    }  }

These values need to be populated when deploying the template. When you deploy the template the parameters will be parsed and added to the command which can be viewed through the IntelliSense in the PowerShell ISE or by tabbing. For example:

Make sure all parameters are populated for the template. If you miss parameters you will be prompted for them, for example:

PS D:temp> New-AzureResourceGroup -Name TestRGDeploy -Location "East US" -GalleryTemplateIdentity Microsoft.WindowsServer2012Datacenter.0.2.77-preview cmdlet New-AzureResourceGroup at command pipeline position 1Supply values for the following parameters:(Type !? for Help.)newStorageAccountName: 

The same applies to templates downloaded from GitHub except -TemplateFile is used instead of -GalleryTemplateIdentity, for example:

New-AzureResourceGroup -Name TestRGDeploy -Location "East US" -TemplateFile D:temp101-simple-windows-vm.json

If you downloaded the parameter file and populated it with values you would need to pass this using the -TemplateParameterFile parameter.

Microsoft has a good article on ARM with PowerShell at https://azure.microsoft.com/en-us/documentation/articles/powershell-azure-resource-manager/ which should be reviewed. Also see https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-azurerm-versus-azuresm/ for general information.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like