What do you do when you want to continuously test your code in environments that match your production environment in a way that is cost-effective? Or, what do you do when you need to deploy the same type of resource multiple times, such as front-end web servers? The answer to those questions is that you use Azure Resource Manager (ARM) Templates.

ARM Templates are JSON files that allow you to declare what you want your resources and environment to look like. These templates are deployed using several PowerShell commands that are part of the Azure PowerShell Module. Deploying your Azure resources this way allows you to declare your environment and then make it so.

There are two deployment modes for ARM Templates, Incremental and Complete. In the Complete mode, Resource Manager deletes resources that exist in the resource group but are not specified in the template. In Incremental mode, Resource Manager leaves unchanged resources that exist in the resource group but are not specified in the template. By default, ARM Templates are deployed in Incremental mode.

There are also many extremely powerful functions available for use in ARM templates such as CopyIndex, which increments resources for you. This function allows you to declare a Virtual Machine (VM) once in a template, and then Azure will deploy the VM as many times as you specify and increment the name each time (MyVM1, MyVM2, MyVM3, etc). This saves a lot of time and lines of code in your ARM template because you only need to declare the resource and its dependencies one time, instead of for every single VM you want to provision. You can also use PowerShell Desired State Configuration to configure your VM post-deployment, saving even more time!

Using the common scenario of deploying a new virtual machine (VM) in Azure, let’s take a look at how we would do that with an ARM template. When you deploy a new VM in Azure you aren’t deploying just a VM. You also need a storage account (for diagnostics or disks), a virtual network, a NIC and a public IP address. An ARM Template for this scenario would look like the example below.

 

At first glance ARM Templates may look complicated, however, they are easy to read and once you start working with ARM templates you will come to appreciate how powerful they are. Using a tool like Visual Studio Code along with its extensions for Authoring ARM Templates makes it a breeze to read and configure ARM templates.

Additional Resources: