- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
Azure Resource Manager (ARM) templates are a way you can deploy many different Azure resources (VMs, virtual networks, public IP addresses, storage accounts, etc.) at one time. They allow you to define one or more Azure components you’d like to deploy in a text file, with text structured in a specific way, and use that file to define all of the needed components.
An ARM template is a simple JSON file with some functionality added to it for ARM purposes through expressions, functions, parameters, and variables. Expressions and functions allow you to essentially write basic “code” in JSON to calculate various attributes on the fly. Parameters allow you to pass various attributes at the command line directly to the template, whereas variables allow you to define certain attributes all in one place and reuse those values throughout the template.
In its most basic structure, an ARM template looks something like this:
{ "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "", "parameters": { }, "variables": { }, "resources": [ ], "outputs": { } }
$schema, contentVersion, and resources are the only blocks that are required. $schema will nearly always be the same as you see above because it defines the specific JSON format that Azure requires. contentVersion will probably always be 1.0.0.0 as well. The most important part of ARM templates are resources. Resources represent the various components that you’d like to deploy, such as storage accounts, databases, virtual networks, and VMs. Each resource is defined by a few different properties. Here’s an example of creating a public IP address in Azure with straight JSON, with no extra expressions, functions, variables, or parameters:
{ "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [ { "apiVersion": "2015-05-01-preview", "type": "Microsoft.Network/publicIPAddresses", "name": "MyPublicIp", "location": "westus", "properties": { "publicIPAllocationMethod": "Dynamic", "dnsSettings": { "domainNameLabel": "mydomain" } } ], }
The only required properties for a resource are apiVersion, type, and name. apiVersion represents the Azure API version to use, and type is the kind of resource you’re defining. You can see in this example that I’m defining a public IP address called MyPublicIP located in the West US data center. Because I’d like to define some properties for this public IP address, I’m making it dynamic, which will grab any available IP and assign the domain name label. The properties node allows you to define various attributes about the particular resource.
I’ll save this template to my local machine and call it DeployPublicIP.json. To deploy this template to my resource group WebApp1, I’ll use the cmdlet New-AzureRmResourceGroupDeployment that comes with the Azure PowerShell module:
New-AzureRmResourceGroupDeployment -ResourceGroupName WebApp1 -TemplateFile C:\Templates\DeployPublicIP.json
Parameters are another nice feature of templates. In my example above, I have all of the values filled in statically. However, what if you’d like to build a shared template that can build different public IP addresses? You could use parameters, like this:
{ "apiVersion": "2015-05-01-preview", "type": "Microsoft.Network/publicIPAddresses", "name": parameters('name'), "location": parameters('location'), "properties": { "publicIPAllocationMethod": "Dynamic", "dnsSettings": { "domainNameLabel": "mydomain" } } }
In the example above, I’ve converted the name and location attributes to parameters by replacing them with the keyword “parameters” followed by a set of parentheses with the parameter name inside. Notice that I also removed the double quotes. This indicates this is an expression that will be evaluated at run time to replace whatever I specify for the parameter name as the actual public IP address name.
To use parameters, you must specify the values at run time rather than statically in the JSON file itself. To do this, the New-AzureRmResourceGroupDeployment cmdlet reads from the template file provided and automatically creates parameters to that cmdlet where you can fill in those values. In this example, I’d specify the name and location parameters by simply using them as parameters to the New-AzureRmResourceGroupDeployment cmdlet:
New-AzureRmResourceGroupDeployment -ResourceGroupName WebApp1 -TemplateFile C:\Templates\DeployPublicIP.json –name MyPublicIP –location westus
This would replace the public IP address’s name and location inside the template when Azure receives it.
Subscribe to 4sysops newsletter!
We just went over a basic example of using ARM templates. If you’d like more information about building your own templates, head on over to the Authoring Azure Resource Manager templates post on the Azure website.