Using ARM templates to provsion resources in Azure

Introduction

Azure Resource Manager (ARM) templates are powerful tools for automating the deployment of Azure resources. Written in JSON, ARM templates define the infrastructure and configurations, enabling Infrastructure as Code (IaC) practices. Lets look at ARM templates, their benefits, and practical examples to get started.

Why Use ARM Templates?

  1. Declarative Syntax: Define "what" to deploy, and Azure handles the "how."

  2. Provision Multiple Resources: ARM templates can provision one or multiple resources simultaneously, making them ideal for complex deployments like setting up virtual networks with storage and compute.

  3. Repeatable Deployments: Ensure consistency across environments.

  4. Built-in Validation: Verify templates before deployment.

  5. Modularity and Reusability: Break templates into smaller reusable components.

  6. Integration with CI/CD: Seamlessly integrate with Azure DevOps pipelines.

ARM templates are part of a broader category of Infrastructure as Code (IaC) tools, which also include Bicep, Terraform, and Ansible, providing diverse options for managing cloud infrastructure or environments.

Basic Structure of an ARM Template

An ARM template has the following sections and you would typically use VS Code or other editors to create or modify it:

  • parameters: Inputs to the template.

  • variables: Values derived from parameters or static inputs.

  • resources: Resources to deploy.

  • outputs: Information returned after deployment.

Example: Deploying an Azure Storage Account

Below is a sample ARM template to deploy a storage account (A service that can be used for uploading and storing unstructured data such images, videos and documents):

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string",
      "defaultValue": "mystorageaccount",
      "metadata": {
        "description": "sadocuments"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "East US",
      "allowedValues": ["East US", "West US", "Central US"],
      "metadata": {
        "description": "Location of the storage account"
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ],
  "outputs": {
    "storageAccountName": {
      "type": "string",
      "value": "[parameters('storageAccountName')]"
    }
  }
}

Deploying the Template

You can deploy ARM templates using one of the following ways:

  1. Azure Portal: Upload the template in the "Deploy a Custom Template" section. You can modify the template as well as parameters. This uses UI.

  2. Azure CLI: You can learn these commmands using our Azure CLI cheatsheet. You can run this command from CloudSheell or from your local machine

     az deployment group create --resource-group <ResourceGroupName> --template-file <TemplateFilePath>
    
  3. Azure PowerShell: You can learn these commmands using our Azure Powershell cheatsheet. You can run this command from CloudSheell or from your local machine

     New-AzResourceGroupDeployment -ResourceGroupName <ResourceGroupName> -TemplateFile <TemplateFilePath>
    

Best Practices

  • Use modular templates for reusability.

  • Having a separate parameter file will make it easy to segregate sections.

  • Leverage what-if analysis to preview changes before deployment.

  • Store templates in version-controlled repositories for collaboration.

ARM templates exemplify the power of IaC, providing a robust solution for provisioning Azure resources alongside other tools like Bicep and Terraform.

To dive deeper, explore Microsoft's official documentation on ARM templates