Azure ARM templates (Infrastructure as a code)
In Azure and Azure Stack, we can provision resources using the Infrastructure as a code concept. The Azure Resource Manager (ARM) is the management layer (API) which we use to connect for deploying Azure resources like Resource Group, Virtual Machine, App Service Plan, Storage, App Service etc.
In this blog we are going to look at the below:
- Structure of Azure Resource Manager Template
- Sample ARM Template
- Preferred Editor for ARM Template
- Add Resource to ARM Template
- Preview ARM Template Graphically
- Deploy resources in Azure using ARM Template.
Structure of ARM template
ARM templates are files with .json extension. The main elements of ARM template are the following:
- $schema
- contentVersion
- parameters
- variables
- resources
- outputs
{
“$schema”: “https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {},
“functions”: [],
“variables”: {},
“resources”: [],
“outputs”: {}
}
Element | Mandatory | Description |
$schema | Yes | Location of the JSON schema file that describes the version of the template language. |
contentVersion | Yes | Version of the template (such as 1.0.0.0). When deploying resources using the template, this value can be used to make sure that the right template is being used. |
parameters | No | Values that are provided when deployment is executed to customize resource deployment. |
variables | No | Values that are used as JSON fragments in the template to simplify template language expressions. |
resources | Yes | Types of services that are deployed or updated in a resource group. |
outputs | No | Values that are returned after deployment. |
Sample ARM template
The below sample ARM template has two resources: App Service Plan (F1: Free sku) and App Service which depends on the F1 sku plan.
{
“$schema”: “https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {},
“functions”: [],
“variables”: {},
“resources”: [
{
“name”: “appServicePlan1”,
“type”: “Microsoft.Web/serverfarms”,
“apiVersion”: “2018-02-01”,
“location”: “[resourceGroup().location]”,
“sku”: {
“name”: “F1”,
“capacity”: 1
},
“tags”: {
“displayName”: “,”
},
“properties”: {
“name”: “appServicePlan1”
}
},
{
“name”: “az007webApp1”,
“type”: “Microsoft.Web/sites”,
“apiVersion”: “2018-11-01”,
“location”: “[resourceGroup().location]”,
“tags”: {
“[concat(‘hidden-related:’, resourceGroup().id, ‘/providers/Microsoft.Web/serverfarms/appServicePlan1’)]”: “Resource”,
“displayName”: “webApp1”
},
“dependsOn”: [
“[resourceId(‘Microsoft.Web/serverfarms’, ‘appServicePlan1’)]”
],
“properties”: {
“name”: “az007webApp1”,
“serverFarmId”: “[resourceId(‘Microsoft.Web/serverfarms’, ‘appServicePlan1’)]”
}
}
],
“outputs”: {}
}
Preferred Editor for ARM Template
Visual Studio Code is my favorite editor when it comes to create and manage ARM templates. Using the following extension in the VS Code, the life of ARM author can become really easy.
ARM Template Viewer
This extension displays a graphical preview of Azure Resource Manager (ARM) templates. The view will show all resources with the official Azure icons and also linkage between the resources.
Azure ARM Template Helper
This extension has a tree viewer like the arm tools for visual studio except this viewer enumerates all the properties of a resource instead of just the resources.
Another killer feature is the ability to test arm template functions locally so we can test any scripts in the templates without having to deploy them
A new killer feature of this extension is the ability to draw a graph of the dependencies between resources in a template.
Azure Resource Manager Tools
This extension provides language support, resource snippets, and resource auto-completion to help us create and validate Azure Resource Manager templates.
When in an empty JSON file, typing arm produces a list of scaffolding snippets. These snippets can be used to create an empty template for any ARM deployment scope (Tenant, Subscription, Management Group, and Resource Group).
Add Azure resource to an ARM template
Once we start working in an ARM template, placing our cursor in the resource and typing arm, CTRL + Space, or { produces a list of 70+ snippets for Azure resources.
Selecting a snippet adds an instance of the resource type to the ARM template. For example, if you want to create a storage account resource, just type arm-storage
And the following resource type will be automatically generated for you:
{
“$schema”: “https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {},
“functions”: [],
“variables”: {},
“resources”: [
{
“name”: “storageaccount1”,
“type”: “Microsoft.Storage/storageAccounts”,
“apiVersion”: “2019-06-01”,
“tags”: {
“displayName”: “storageaccount1”
},
“location”: “[resourceGroup().location]”,
“kind”: “StorageV2”,
“sku”: {
“name”: “Premium_LRS”,
“tier”: “Premium”
}
}
],
“outputs”: {}
}
Deploy an ARM Template
From the VS Code Terminal, you can connect to your Azure subscription using CLI commands.
Az login
Once you logged to your account, set the account to the subscription you like
az account set –subscription 44b1k6vg-3d27-5408-762-77dec12344af
Now you can validate your ARM template to make sure there is no error. (I have used the sample ARM template code given above.)
az deployment group validate –resource-group ARM-Demo –template-file C:\ARM-Templates\AzureDeploy.json
If there is no error during validation, you are now good to create the resources in Azure using the template:
az deployment group create –resource-group ARM-Demo –template-file C:\ARM-Templates\AzureDeploy.json
As you can see from the Azure Portal, the Resource group is now hosting two resources: Azure App Service Plan and Azure App Service.