Cost Saving - Auto Shutdown For Azure VMs

Need for Auto Shutdown Feature

  • VM prices are being charged when they are in the running status and are even not being used after business hours and during the weekends.
  • The cost of Azure VMs can be reduced by turning off the VMs automatically after business hours.
  • Azure VMs will be turned on the next working day whenever the team wants to work on the VMs.

Technical Implementation to Enable Auto Shutdown Feature for VM

Add a PowerShell task in the stages of the release pipeline which will work as below,

  • Sets the subscription to use in the current session.
  • Set Time for Auto Shut Down and Time Zone,

    shutdown_time = "1730"
    shutdown_timezone = "W. Europe Standard Time"

  • Check the number of VMs in the resource group.
    • If No of VM = 0 then do nothing.
    • If No of VM > 0 then perform below for each VMs,
  • Check Tag DoNotAutoShutdown of Azure VM is Yes then do nothing.
  • If Tag DoNotAutoShutdown is not present then set the below properties to enable the Auto Shutdown Feature.
    • Set ComputeVmShutdownTask for a given shutdown_time, shutdown_timezone.
    • Set notification settings for email alert before time in minutes(30) and emailRecipient.
  • Creates resource 'microsoft.devtestlab/schedules' to enable auto shutdown.

Github link for PowerShell script 

https://github.com/sagu94271/seaa/blob/main/Devops/Powershell/autoshutdownazurevm.ps1

Conclusion

We can save a huge amount by enabling the auto shutdown feature when working with multiple azure VMs.

Feel free to share your feedback :)


Next Recommended Reading Azure Active Directory Tenant ID