Azure Virtual Desktop, cost optimisation
Using Azure Virtual Desktop (AVD) with Windows 10/11 Enterprise Multi-Session you can save money. Through AVD optimization, you can half these costs.
Author:
bha created on 2023-08-23
Image: techcommunity.microsoft.com
Azure VM Cost Optimization
When Microsoft Azure Virtual Machine (VM) is running, it is billed on an hourly basis. You only pay for the number of hours your VM runs after it is created.
As you may already know, if you simply shut down a VM, it continues to cost money. In this case the VM has the status 'Stopped'. To save money you can stop the VM on Azure Portal, then it will have the status 'Stopped (Deallocated)'.
Microsoft offers a simple setting to automatically stop a VM for a fixed time. On the VM setting, look for 'Auto-shutdown'.
This article is about a lot more, namely about dynamic stopping (Deallocate) of
Azure Virtual Desktop (AVD) when no user is working on it anymore. For a personal AVD you can find enough information on the net how to switch off such a VM.
For
pooled AVD, on the other hand, nothing or hardly anything can be found. This post shows you how to automatically turn off a multi-seassion AVD when not in use, so that no costs are caused for it.
Automatically turning off AVD only makes sense if users can easily turn it on when needed. Of course this is possible and there are already good instructions from Microsoft see
Start VM on Connect.
Preparations on Windows 10/11
On Windows 10 or 11, settings are made to turn off the AVD if no active user is working. This detection is best done by each AVD. Here is shown how to set this directly. Of course you can also do this through GPO or Intune for multiple VMs.
Edit group policyLocal Computer Policy >> Computer Configuration >> Administrative Templates >> Windows Components >> Remote Desktop Services >> Remote Desktop Session Host >> Session Time Limits. Activate 'Set time limit for active but idle Remote Desktop Services sessions' and select a time e.g. 1h.
The other session time limits can be enabled, but are not a requirement in this case.
Task schedulerThis will e.g. search from 5 PM if the number of active users is 0, if yes, AVD will shutdown after 2 minutes warning. The users with disconnected session are not counted.
If No, then the batch script will check every hour for the next 12 hours whether AVD can be shut down.
For the setting see pictures 1 and 2. With this task a CMD file with the following contents should be executed:
These settings can be applied both to pooled AVD Multiseassion, used by multiple users, and to personal AVD, used by only one user.
Image 1: Task Scheduler creation
Image 2: Task Scheduler trigger
Azure Portal Settings
After the VMs are shut down, either by script as in the case of AVD or manually, such VMs are deallocated via Azure Automation Accounts so that no costs are incurred.
To do this, create an Automation Accounts, best to search it and create this resource by default, see Image 3.
Then check or edit automation accounts as follows:
Modules
Check if the modules A.Accounts and Az.Compute are available. If this is not the case then >> Add a module >> Browse from gallery.
Identity
Check if the status is 'On', if necessary set so, and >> Azure role assignments >> Add role assignment >> Scope: Subscription >> Role: 'Desktop Virtualization Power On Off Contributor' is to be added.
Runbooks
Create a runbook >> Name: (e.g. RB-Deallocate-stopped-VMs), Runbook type: PowerShell, Runtime version: 5.1
After creating runbook named 'RB-Deallocate-stopped-VMs' >> Edit >> Edit in Portal and add the following content:
Test it via >> Test pane >> Start >> Close >> Publish.
Schedules
To run the successfully tested runbook, e.g. to check for shutdown VMs every hour, you need to create a schedule as follows:
Select runbook named 'RB-Deallocate-stopped-VMs' >> Link to schedule >> Schedule >> Add a schedule >> New Schedule, Name: (e.g. Each1hour), Start time: (e.g 00:30), Recurring: 1 Hour >> Create >> OK.
Activity log
When a VM is 'Stopped (deallocated)' by this script, this can be seen in the activity log of the corresponding VM, see Image 4. This also logs that AVD was started by the user via the usual client (Windows Virtual Desktop).
Image 3: Azure Automation Accounts creation
Image 4: VM Activity log
Further optimization
This compact runbook script searches in all resource groups for all VMs that are shut down, have status 'PowerState/stopped', in order to set in the state 'Stopped (Deallocated)'.
The solution with hourly schedule may be too inaccurate for you, as on average it takes half an hour until VMs are 'properly' switched off. There are 2 alternatives to control Automation Accounts.
With Azure Monitor alert rule that kick in as soon as VM shuts down. There is also the possibility to send email notifications. Alerts and notifications cost something, although it is not much.
A better alternative are Webhooks. This allows the VM itself to announce the shutdown by calling the webhook URL. Withit you can send parameters such as computer name. The process with Webhooks is a bit complex, but it is customizable and you will end up with an effective solution.
At the Windows level, further optimizations can be made, such as:
A shutdown warning (2 minutes, see above) is shown during the shutdown. If a user logs in during this time, the shutdown can be aborted with (shutdown /a).
Instead of the proposed time from 5 PM (Image. 2), a dynamic time can also be set that starts when the computer boots up. For example by
following settings Task Scheduler >> At startup >> Delay: 1 hour >> Repeat 1 hour >> Duration 1 day.
Please note that costs for storage or public IP addresses still exist. Dynamic IP address may
change due to this. To prevent it convert dynamic IP address to static.
We have several of these solutions in use at our customer's AVD pools. The annual savings for a large network are immense. For questions or suggestions for improvement we are very grateful, please use the contact form below.