October 17, 2023

Azure VMs – How To Go Green

This article will help guide you to go green with your Azure VMs and save some green! 💸

When building a cloud solution, many different items need to be considered. One of these items, which will help guide you in the right direction, would be following the Azure Well-Architected Framework. This is a framework all cloud professionals should be informed about. If your solution is already deployed, take the Assessment Review and see where you can improve:

The Azure Well-Architected Framework Pillars:

  • cost management
  • operational excellence
  • performance efficiency
  • reliability
  • security

On top of that, Microsoft is also making considerable steps towards sustainability and aligning with Green Software Principles from the Green Software Foundation.

Today, we will focus on the sustainability and cost optimizations for Azure VMs.

Go Green with Automation to Stop and Start Your VMs

Depending on your workload, you may need to start and stop your VMs on a schedule. A use case can be development servers that only need to run during business hours.

There is already an Auto-shutdown feature within the VM settings within the Operations section. However, there is no Auto-start feature as of writing this article.

Go Green with Automating Start and Stop of your VMs

Within Auto-shutdown, you simply configure a few properties:

There are many options to achieve the Auto-start functionality, but two of the most common and quickest to implement are Automation Tasks and Logic Apps.

With Automation Tasks, you configure Runbooks within your Automation Account and can browse the gallery for a plug-and-play solution. As of the time of this article, Azure Automation Run As Account will retire on September 30, 2023. So be sure to use a Managed Identity if you opt for this solution.

If you go this route, one of the most popular templates looks like the graph below:

Go Green using Azure Logic Apps to start and stop your VMs.

I would opt for a Logic App. They are very user-friendly and have an abundance of connectors and functionality. You can configure them directly within the individual VM settings, but I would not advise doing so.

If you were to configure them per VM, this is how you would do it.

Go to the Automation section of the VM:

Add a task:

Select a template:

Authenticate:

Configure the task:

Creating them in the individual VM is very easy but a pain in the ass to manage since it will create a Logic App per VM. When you start seeing the same logic repeatedly, that should be a clue that you are not abiding by the DRY (Don’t Repeat Yourself) principle.

We will use parameterization to make the Logic App easier to build and manage. We will skip the steps needed to create the Logic App resource. Here is a link to guide you through those initial steps and other information around Logic Apps: Overview – Azure Logic Apps | Microsoft Learn.

For the parameterization, we will create the following parameters:

  • EmailID (string): semi-colon separated list of email addresses
  • Frequency (string): set this as “Day”
  • Interval (integer): set this as “1”
  • Notify (bool): set this as “true”
  • StartTime (string): set this as the first day you want the schedule to start
  • TimeZone (string): set this as the wanted Time Zone
    • VmList: (array): this is an JSON object with the following schema:
      {"resourceGroup": "string", "resourceVM": "string", "subscriptionId": "string"}

The first step of developing the Logic App would be to create the trigger. We will create a “Recurrence Trigger” since we are building this on a schedule. You could also make this event based on your needs with an HTTP Request, email, Service Bus, etc.

Next, we will want to loop on the VmList array. For each iteration, we will want to execute the following logic:

  • Start the VM: passing subscriptionId, resourceGroup, and resourceVM parameters
  • Send Email on Failure or Success if “Notify” parameter is “true.”

For Each Loop:

Notifications:

There you have it. A Logic App set up on a schedule to start VMs on the frequency you decide! If any changes need to be made, you can tweak your parameters. One resource with a friendlier interface with an abundance of connectors/controls/functionality to simply start/stop your VMs. As stated, there are many ways to accomplish this task, but driving towards cost optimizations and less computing; is a great solution.

If you need help saving money on your Azure subscriptions, contact us!

Happy coding!

🐵

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.