Lesson 1 of 5 ARM Templates and Bicep Files

Reading Azure Resource Manager Templates

ARM templates declare resources in JSON. This lesson explains the template elements, expressions and dependencies so that templates can be read confidently.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the top-level elements of an ARM template.
  • Explain template expressions and common functions.
  • Explain how dependencies control deployment order.

Declarative deployment with Azure Resource Manager

Azure Resource Manager is the deployment and management service for Azure: the portal, the Azure CLI, Azure PowerShell and SDKs all send requests to it. A template is declarative: it describes the desired end state rather than the steps, and Resource Manager works out the order, deploys resources in parallel where possible and is idempotent, so deploying the same template twice produces the same result.

Template elements

An ARM template has the following top-level elements. Only $schema, contentVersion and resources are required.

ElementPurpose
$schemaThe JSON schema, which also indicates the deployment scope (resource group, subscription, management group or tenant)
contentVersionA version number for the template, such as 1.0.0.0
parametersValues supplied at deployment, with type, defaultValue, allowedValues, minValue, maxValue, minLength, maxLength
variablesValues computed in the template, usually from parameters and functions
functionsUser-defined functions
resourcesThe resources to deploy, each with type, apiVersion, name, location, properties and optional dependsOn
outputsValues returned after deployment, such as a resource ID or IP address
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageName": { "type": "string", "minLength": 3, "maxLength": 24 },
    "skuName": { "type": "string", "defaultValue": "Standard_LRS",
                 "allowedValues": [ "Standard_LRS", "Standard_GRS" ] }
  },
  "variables": { "location": "[resourceGroup().location]" },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2023-05-01",
      "name": "[parameters('storageName')]",
      "location": "[variables('location')]",
      "sku": { "name": "[parameters('skuName')]" },
      "kind": "StorageV2",
      "properties": { "minimumTlsVersion": "TLS1_2" }
    }
  ],
  "outputs": {
    "blobEndpoint": { "type": "string",
      "value": "[reference(parameters('storageName')).primaryEndpoints.blob]" }
  }
}

The following diagram shows an ARM template excerpt and how it behaves when given a VM size that the template does not allow.

How the template behaves with vmSize Standard_D4s_v5. The parameter allows only Standard_B2s and Standard_D2s_v5, so validation fails before any resource is created. With an allowed size, the VM's dependsOn on the network interface makes Resource Manager create the NIC first and then the VM.
The template is abbreviated.
Match the pairs

Template elements

Exam skills 3.1.1

Match each ARM template element with what it holds.

Log in to try this activity Interactive activities are free with an account.

Expressions, functions and dependencies

Values in square brackets are template expressions evaluated at deployment. Common functions are parameters() and variables(); resourceGroup() and subscription() for context such as location and ID; concat(), format() and uniqueString() for names; resourceId() to build a resource's ID; and reference() to read a deployed resource's runtime properties.

When one resource needs another, such as a network interface that needs a subnet, the dependent resource lists the other in dependsOn (by name or resourceId()), and Resource Manager deploys them in order. Using reference() or listKeys() on a resource in the same template also implies a dependency. Parameters of type securestring and secureObject are not logged or returned in deployment history, which is how passwords are passed.

Scenario questions

Reading template expressions

Exam skills 3.1.1

Choose the template function or property that does each job.

Log in to try this activity Interactive activities are free with an account.
Worked example

Reading a template

A template has a parameter vmSize with allowedValues ["Standard_B2s", "Standard_D2s_v5"] and no default, a variable nicName set to [concat(parameters('vmName'), '-nic')], and a VM resource with dependsOn containing [resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]. What happens if an administrator deploys it with vmSize Standard_D4s_v5, and in what order are the resources created?

The deployment fails validation before any resource is created, because Standard_D4s_v5 is not in the allowed values; the administrator must choose one of the two listed sizes. With a valid size, Resource Manager creates the network interface named, for example, vm1-nic first and then the VM, because the VM's dependsOn references the NIC.

Check yourself

Which three elements are required in every ARM template?

Show answer

$schema, contentVersion and resources.

Exam Tips

Remember for the exam

  • Parameters are supplied by the deployer; variables are computed in the template.
  • dependsOn controls deployment order; reference() implies a dependency.
  • allowedValues violations fail validation before deployment.
  • Use securestring for passwords so they are not logged.