MSDN Documentation

Azure | Virtual Machines | Tutorials

Deploying Azure Virtual Machines with ARM Templates

This tutorial will guide you through the process of deploying and managing Azure Virtual Machines (VMs) using Azure Resource Manager (ARM) templates. ARM templates are JSON files that define the infrastructure and configuration for your Azure solutions.

On this page:

Introduction to ARM Templates

ARM templates provide a declarative way to define your Azure infrastructure. Instead of imperative steps (like scripts), you declare what you want to deploy, and the Azure Resource Manager service handles the deployment.

Prerequisites

Before you begin, ensure you have the following:

Deploying a Basic VM

Let's start by deploying a simple Windows VM. We'll use a predefined ARM template for this.

Template Structure

A typical ARM template consists of the following sections:

Example Basic VM Template

{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "vmName": { "type": "string", "defaultValue": "myVM", "metadata": { "description": "Name of the virtual machine." } }, "adminUsername": { "type": "string", "defaultValue": "azureuser", "metadata": { "description": "Username for the administrator account." } }, "adminPassword": { "type": "securestring", "metadata": { "description": "Password for the administrator account." } }, "vmSize": { "type": "string", "defaultValue": "Standard_DS1_v2", "metadata": { "description": "Size of the virtual machine." } } }, "resources": [ { "type": "Microsoft.Compute/virtualMachines", "apiVersion": "2020-06-01", "name": "[parameters('vmName')]", "location": "[resourceGroup().location]", "properties": { "hardwareProfile": { "vmSize": "[parameters('vmSize')]" }, "osProfile": { "computerName": "[parameters('vmName')]", "adminUsername": "[parameters('adminUsername')]", "adminPassword": "[parameters('adminPassword')]" }, "storageProfile": { "imageReference": { "publisher": "MicrosoftWindowsServer", "offer": "WindowsServer", "sku": "2019-Datacenter", "version": "latest" }, "osDisk": { "createOption": "FromImage", "managedDisk": { "storageAccountType": "Standard_LRS" } } }, "networkProfile": { "networkInterfaces": [ { "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('vmName'), '-nic'))]" } ] } } }, { "type": "Microsoft.Network/virtualNetworks", "apiVersion": "2020-07-01", "name": "[concat(parameters('vmName'), '-vnet')]", "location": "[resourceGroup().location]", "properties": { "addressSpace": { "addressPrefixes": [ "10.0.0.0/16" ] } } }, { "type": "Microsoft.Network/virtualNetworks/subnets", "apiVersion": "2020-07-01", "name": "[concat(parameters('vmName'), '-subnet')]", "location": "[resourceGroup().location]", "dependsOn": [ "[resourceId('Microsoft.Network/virtualNetworks', concat(parameters('vmName'), '-vnet'))]" ], "properties": { "addressPrefix": "10.0.0.0/24" } }, { "type": "Microsoft.Network/networkInterfaces", "apiVersion": "2020-07-01", "name": "[concat(parameters('vmName'), '-nic')]", "location": "[resourceGroup().location]", "dependsOn": [ "[resourceId('Microsoft.Network/virtualNetworks/subnets', concat(parameters('vmName'), '-vnet'), concat(parameters('vmName'), '-subnet'))]" ], "properties": { "ipConfigurations": [ { "name": "ipconfig1", "properties": { "subnet": { "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', concat(parameters('vmName'), '-vnet'), concat(parameters('vmName'), '-subnet'))]" }, "privateIPAllocationMethod": "Dynamic" } } ] } } ], "outputs": { "vmId": { "type": "string", "value": "[resourceId('Microsoft.Compute/virtualMachines', parameters('vmName'))]" } } }

Deployment Steps (using Azure CLI)

  1. Save the template above as azuredeploy.json.
  2. Create a resource group:
    az group create --name myResourceGroup --location eastus
  3. Deploy the template:
    az deployment group create --resource-group myResourceGroup --template-file azuredeploy.json --parameters adminPassword='YourSecurePasswordHere'
    Note: Replace 'YourSecurePasswordHere' with a strong password. For production, use Azure Key Vault for secrets.

Advanced VM Configurations

ARM templates can define more complex VM setups:

Availability Sets

Ensure your VMs are highly available by distributing them across fault and update domains.

Learn More →

Scale Sets (VMSS)

Deploy and manage a group of identical VMs that scale automatically.

Learn More →

Custom Data & Cloud-init

Run scripts during VM creation for initial configuration.

Learn More →

Managed Disks

Use high-performance managed disks for your VM's operating system and data.

Learn More →

Managing VMs with ARM Templates

ARM templates are not just for initial deployment. You can also use them to:

You can re-run a deployment with an updated template to manage the state of your existing resources.

Next Steps

Ready to build more sophisticated Azure infrastructure? Explore these resources:

Explore ARM Template Functions Learn about Bicep Azure VM Best Practices