Auto shutdown Azure VMs with ARM templates

22/11/2016

Microsoft just released the ability to schedule shutdown of Azure Resource Manager VMs. The feature has previously only been available in dev test labs, but is now also usable for stand alone VMs.

Unfortunately the feature is pretty limited, it can trigger a shutdown at a specific time, and 15 minutes in advance it can call a web hook. But there’s no scheduled start, or ability to set different rules for weekends vs weekdays, so depending on your usage scenario you might still have to rely on other solutions. Hopefully the features will be extended over time.

azure-scheduled-vm-shutdown

If you want to ensure that machines are setup with the scheduled shutdown setup, when deploying from an ARM template, that is also possible. I wasn’t able to find any documentation, so I digged around a little with the ARMClient and figured out how to do it.

The configuration of the scheduled shutdown is a separate ARM resource under the Microsoft.DevTestLab/schedules resource provider namespace. So to add it to the template you just have to add the following resource to your resource collection
[js]
{
"type": "Microsoft.DevTestLab/schedules",
"name": "shutdown-computevm-mydockervm",
"apiVersion": "2016-05-15",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat(‘Microsoft.Compute/virtualMachines/’, variables(‘vmName’))]"
],
"properties": {
"status": "Enabled",
"taskType": "ComputeVmShutdownTask",
"dailyRecurrence": {
"time": "1900"
},
"timeZoneId": "W. Europe Standard Time",
"notificationSettings": {
"status": "Enabled",
"timeInMinutes": 15,
"webhookUrl": "http://sjkp.dk"
},
"targetResourceId": "[resourceId(‘Microsoft.Compute/virtualMachines’,variables(‘vmName’))]"
}
}
[/js]

If you want to see it as part of a full ARM template, you can take a look at the ubuntu-simple-docker template in my github repository.