Automating Virtual Machine Tasks in Azure

This document provides guidance on how to automate common tasks related to Azure Virtual Machines (VMs), enhancing efficiency and reducing manual effort.

Introduction to Azure Automation

Azure Automation is a cloud-based automation and configuration service that provides process automation, configuration management, and update management across your Azure and on-premises environments.

Key Automation Tools and Services

Automating VM Deployment and Configuration

Using ARM Templates/Bicep

Define your VM infrastructure in declarative templates. This ensures consistent deployments and allows for easy version control and rollback.


{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "type": "string",
            "defaultValue": "myAutomatedVM",
            "metadata": {
                "description": "Name of the virtual machine."
            }
        },
        "adminUsername": {
            "type": "string",
            "defaultValue": "azureuser",
            "metadata": {
                "description": "Administrator username for the virtual machine."
            }
        },
        "adminPassword": {
            "type": "securestring",
            "metadata": {
                "description": "Administrator password for the virtual machine."
            }
        }
    },
    "resources": [
        {
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2020-06-01",
            "name": "[parameters('vmName')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "hardwareProfile": {
                    "vmSize": "Standard_DS1_v2"
                },
                "osProfile": {
                    "computerName": "[parameters('vmName')]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "Canonical",
                        "offer": "0001-com-ubuntu-server-focal",
                        "version": "latest",
                        "sku": "20_04-lts-gen2"
                    },
                    "osDisk": {
                        "createOption": "FromImage",
                        "managedDisk": {
                            "storageAccountType": "Standard_LRS"
                        }
                    }
                }
            }
        }
    ]
}
            

Using Azure Automation Runbooks

Write PowerShell scripts to deploy VMs with specific configurations.


$vmConfig = New-AzVMConfig -VMName "MyScriptedVM" -VMSize "Standard_B1s"
Set-AzVMOperatingSystem -VM $vmConfig -Linux -ComputerName "MyScriptedVM" -Credential (Get-Credential)
Set-AzVMSourceImage -VM $vmConfig -PublisherName "Canonical" -Offer "0001-com-ubuntu-server-focal" -Skus "20_04-lts" -Version "latest"
New-AzVM -ResourceGroupName "myResourceGroup" -Location "East US" -VM $vmConfig
            

Automating VM Management Tasks

Starting and Stopping VMs

Schedule the start and stop of VMs to optimize costs.

💡 Use Azure Automation schedules or Azure Functions triggered by time.

Start-AzVM -ResourceGroupName "myResourceGroup" -Name "myVMToStart" -Force
Stop-AzVM -ResourceGroupName "myResourceGroup" -Name "myVMToStop" -Force
            

Applying Updates

Leverage Azure Automation's Update Management feature to automate patching for your VMs.

Backups and Disaster Recovery

Configure backup policies and disaster recovery solutions using Azure Backup and Azure Site Recovery. Automation can help in orchestrating failover and failback processes.

Monitoring and Alerting

Set up alerts based on VM performance metrics or logs. Azure Monitor and Azure Log Analytics are key services for this.

Best Practices

💡 Consider using Azure DevOps or GitHub Actions for CI/CD pipelines that include VM automation tasks.