At present Azure gives you below approaches to reduce compute cost of Azure Virtual Machines with variable usage pattern - such as the ones used for development and testing.
- Stop VMs during off-hours using Virtual Machine Auto-Shutdown.
- Start/Stop VMs using Azure Automation.
These approaches require setup to be done for each individual VM separately. However, if you have lot of VMs spread across multiple resource groups in different subscriptions, setting up and managing these solutions takes lot of work. In other words, these solutions doesn't scale very well.
I wrote an app to solve some of these issues: https://github.com/bablulawrence/vmautostop
Here is how it works :
An Azure Function app runs every minute and gets a list of VMs in the subscriptions that it has access to. It then reads VM metric values -
Percentage CPU and
Network Out and calculates their standard deviation. If the standard deviation is less than the predefined threshold, VM is deemed inactive and a warning email is sent. Subsequently VM is stopped if it continues to be inactive.
Assumption is that the variance/standard deviation of CPU utilization and Network traffic for an inactive VM is lower than an active one. This is certainly seems true for VMs which has users logged in using SSH(Linux) or Remote Desktop(Windows) to performing dev/test activities. However this assumption might not be applicable for VMs running workloads with uniform resource consumption patterns and therefore app might not be useful for such cases.