Introduction

In this post, we are going to talk about Azure Resource Manager (ARM) Templates, a json file which allows us to deploy resources on Azure in a few steps.

What is ARM Template

An ARM template is a JSON (JavaScript Object Notation) script. This script includes the resource(s) that are being deployed to an Azure Resource Group.

When to use an ARM Template

In most cases, resources are provisioned from the Azure Portal. But, what if we want to repeat a deployment more than once? At that point the concept of ARM Template came up, which we can use to make the deployments by running a simple PowerShell script.

How can way Create An ARM Template

There are more than one ways to get started with ARM Template:
  • Azure Portal (From the Automation Script & Custom deployment).
  • GitHub (From the section Azure Quick Templates).
  • Visual Studio.
  • Visual Studio Code.


Azure Portal

The first place we can view an ARM Template is the Azure Portal. There are several ways to work with ARM Templates like Automation Script (when a resource is deployed), custom deployment (deploy resources using common templates, Load templates from GitHub or deploy one from scratch).


Automation Script

From the Virtual Machine left blade, click Automation script. This is the easy way because we can view and download the template just by clicking some buttons. 




Custom deployment

To create a custom deployment we must follow the next steps : Search for the service [Template deployment] - Select and click Create .


 At the next screen we can choose to run a deployment.
  


GitHub

A huge collection of ARM Templates can be found on GitHub. We just need to open an internet browser, type in the address https://github.com/Azure/azure-quickstart-templates, and click Enter. 



Visual Studio

We can also deploy an ARM template from Visual Studio, we just need to follow the next steps. From File - New - Project..., select Visual C# - Cloud - Azure Resource Group and click OK



At the next screen we can select between Visual Studio and GitHub templates as the image below shows.





Visual Studio Code


Visual Studio Code is a free source code editor. One of its greatest features is the Extensions that someone can install from the Extension Marketplace. We can download Visual Studio Code from this link. After the installation completes, we can Install the Azure Resource Manager Tools and start building ARM templates from scratch. 



Analyse An Azure Resource Manager (ARM) Template

Breaking down the template.json

To better understand the template file, we are going to analyse it from the top.
  • Line 2 tells ARM how to validate the structure of the template. "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#".
  • The 3rd line contains the Content Version, which is a Version number for metadata details doesn't impact the deployment.
  • The 4th line is about the parameters of the template, every parameter needs a name and a type.
  • Final, the 5th line is the main component of the ARM template where we type there the resource we want to deploy.


Every template file needs to have a $schema, a content Version and a resources element. The parameters element is optional, and can be replaced by a separate file. Additionally, an ARM Template can include an element called outputs, which specifies any values that are returned from the deployment.


Validate The Schema For The ARM Template

To validate an ARM Template we need to execute the following command using bash.
 
az group deployment validate --resource-group [Resource Group] --template-file .\template.json --parameters .\parameters.json


How To Deploy An ARM Template

In the following example suppose that we have already created an ARM template and are trying to deploy it. When we download the template.zip file and unzip it we can see the following files :
  • deploy.ps1 : A powershell script for template deployment.
  • deploy.sh : A bash script that will deploy the ARM template.
  • deployer.rb : A ruby script that will deploy the ARM template.
  • DeploymentHelper.cs : A C# file with the template deployment.
  • parameters.json : A JSON file that includes all the parameters for the deployment.
  • template.json :  A JSON file that include the main deployment.
  After we do all the necessary configuration in the parameters.json and template.json files, we execute the following script.

Login-AzureRmAccount
 
Set-AzureRmContext -SubscriptionId 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeee'
 
New-AzureRmResourceGroup -Name ResourceGroupName -Location "West Europe"
 
New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName ResourceGroupName `
  -TemplateFile C:\MyTemplates\template\template.json `
  -TemplateParameterFile C:\MyTemplates\template\parameters.json


The results after the successful execution:   

See Also