Performing deployments using ARM with templates
One of the most important features of any computer system is the ability to introduce changes to it seamlessly and in an automatic fashion. When infrastructure is considered, it is especially handy if there is a way to code it. This enables us to replicate it anywhere at any time with a single command. This approach, often abbreviated as Infrastructure-as-Code (IaC), plays a major role in modern applications as it guarantees that all the components are scripted and that no manual work is required to restore them (for example, in the case of disaster recovery).
This section addresses this problem by introducing Azure Resource Manager (ARM) templates, which are JSON files that are used to describe how a service should behave and be configured.
There are multiple ways to prepare an ARM template, which can be reused and modified anytime you want. Before we get started, you have to understand how a typical template is structured. Let's take a look at the following JSON file:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "",
"parameters": { },
"variables": { },
"functions": [ ],
"resources": [ ],
"outputs": { }
}
As you can see, it contains two metadata fields ($schema and contentVersion) and five sections. Each section is used for a different purpose:
- parameters: Since each template can be used for different regions, tiers, or configuration settings, there has to be a way you can pass them. This section describes each parameter that can be passed to the template, including its type and the default value.
- variables: Sometimes, you need to evaluate a value based on the passed parameters (or maybe you just want to avoid duplicates and define it in one place). When using ARM templates, this section can be used for defining values that you can use for other resources. As it is parsed after parameters, you can leverage each value that's passed from the outside of the template.
- functions: A user-defined function that you can use in the template.
- resources: The main section in a template. It contains descriptions of the resources you want to provision in Azure. To make things more flexible, you can use values from three other sections: parameters, variables, and functions.
- outputs: This section can be used to return values from your template. Such a value could be a connection string to a storage account, resource ID, or a VM IP address.
Of course, not all sections are required – to make a template valid, it has to contain the following sections:
- $schema
- contentVersion (for versioning purposes)
- resources
All other sections (parameters, variables, functions, and outputs) are optional. A full description of the structure and the syntax can be found at https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authoring-templates.
Now that you have understood the basics of ARM templates, we can try to prepare some and deploy them. As we mentioned previously, there are a few ways to generate or create such a template – we will give each one a go. To make things easier, we will assume that we want to deploy two storage accounts, with the second one related to the first one.
Now that you understand the theoretical concepts of resource groups, we will focus on writing an actual template and using it.