Daniel's Tech Blog

Cloud Computing, Cloud Native & Kubernetes

Integrate auto-shutdown configuration in ARM template deployments for Azure VMs

In November 2016 Microsoft introduced the auto-shutdown feature to Azure VMs which was originally available in Azure DevTest Labs.

-> https://azure.microsoft.com/en-us/updates/set-auto-shutdown-within-a-couple-of-clicks-for-vms-using-azure-resource-manager/

The auto-shutdown feature can be simply enabled through the Azure portal after a VM is deployed.

auto-shutdown01

If you are doing a lot of VM deployments, an Azure Resource Manager template is the way to go. Enabling and configuring the auto-shutdown feature through ARM templates is easy.

Just add the following lines to your ARM template for VM deployments and modify it appropriately. You should place the lines of code in the resources section of the VM resource.

auto-shutdown02

{
    "apiVersion": "[providers('Microsoft.DevTestLab','labs').apiVersions[0]]",
    "type": "microsoft.devtestlab/schedules",
    "name": "[concat('shutdown-computevm-',parameters('vmName'),copyIndex(parameters('numerationOfVMs')))]",
    "location": "[resourceGroup().location]",
    "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/',concat(parameters('vmName'),copyIndex(parameters('numerationOfVMs'))))]"
    ],
    "properties": {
        "status": "Enabled",
        "taskType": "ComputeVmShutdownTask",
        "dailyRecurrence": {
            "time": "1900"
        },
        "timeZoneId": "W. Europe Standard Time",
        "notificationSettings": {
            "status": "Disabled",
            "timeInMinutes": 15
        },
        "targetResourceId": "[resourceId('Microsoft.Compute/virtualMachines',concat(parameters('vmName'),copyIndex(parameters('numerationOfVMs'))))]"
    }
}

As you can see I am always using the latest API version. This approach is a bit risky, because things in the template can get broken quickly with an newly API version released on the Azure platform. So the latest API version right now is 2017-04-26-preview and this version works without any issues.

Beside the API version you also have to modify name, dependsOn and targetResourceId to get it running in your ARM template.

Name is the name of the resource as shown in the Azure portal or through API calls after the resource is successfully deployed.

auto-shutdown03

Last but not least, we need dependsOn to specify the dependency on the VM. So the VM will be deployed first before we start the configuration. The targetResourceId directly links to the VM we would like to configure with the auto-shutdown feature.

Happy deploying!


Posted

in

WordPress Cookie Notice by Real Cookie Banner