Azure Auto Shutdown Policy Configuration and Best Practices

Author

Reads 426

Computer server in data center room
Credit: pexels.com, Computer server in data center room

Configuring an Azure Auto Shutdown Policy is a straightforward process that involves setting a schedule for your virtual machines to shut down automatically.

The policy can be configured to shut down virtual machines at a specific time every day, including weekends.

To create a shutdown policy, you'll need to specify the virtual machines you want to include, as well as the schedule for shutdown.

The schedule can be set to shut down at a specific time, or it can be set to shut down when a certain condition is met, such as when the virtual machine is not being used.

Azure provides a feature called "Start and Stop VMs during off-peak hours" that can be used to automate the shutdown of virtual machines during off-peak hours.

Configuring Auto Shutdown

To configure auto shutdown in Azure, you can use the Auto-shutdown feature, which allows you to specify a specific time (with time zone) when Azure is to automatically shut down the VM. This feature eliminates the need to manually shut down VMs, reducing the risk of human error.

Credit: youtube.com, Auto Shutdown and Auto Start an Azure VM 🛑

To configure Auto-shutdown, navigate to the Virtual Machine blade for the desired Virtual Machine, click on Auto-shutdown, and configure the specific Time, Time Zone, and desired notification Webhook URL settings, then click Save. If you forget to Stop your VM at the end of the day, or whenever the Auto-shutdown time is configured, it will get Shutdown automatically.

Here are some examples of Auto-shutdown schedules: DescriptionTag valueShut down from 10PM to 6 AM UTC every day10pm -> 6amShut down from 10PM to 6 AM UTC every day (different format, same result as above)22:00 -> 06:00Shut down from 8PM to 12AM and from 2AM to 7AM UTC every day (bringing online from 12-2AM for maintenance in between)8PM -> 12AM, 2AM -> 7AM

Configure Shutdown Schedule

To configure a shutdown schedule, you'll need to tag your VM resource groups with a specific format. The tag format is a string that describes the times the VM should be shut down, such as "10pm -> 6am" or "22:00 -> 06:00".

Credit: youtube.com, How to Schedule Automatic Shutdown Windows Server 2022 #doit_mostafa

You can tag your VM resource groups in the Azure portal by navigating to Browse > Resource Groups, opening a resource group that contains VMs to schedule, clicking the tag icon in the upper right, and entering the tag value in the Key field.

The tag value can be a simple string that describes the times the VM should be shut down, such as "Shut down from 10PM to 6 AM UTC every day". You can also use a more specific format, such as "10PM -> 6AM" or "22:00 -> 06:00".

Here are some examples of tag values you can use:

Remember, any time period not defined as a shutdown time is online time, so the runbook will start the VMs accordingly.

Initial Testing

To validate that your auto shutdown runbook works, you can run an initial test manually and inspect the results. This is easy to do.

Assign a shutdown schedule tag to the VM or resource group you want to use for testing. Give it a schedule that covers the current time, such as today's day of the week, e.g. "Wednesday".

Credit: youtube.com, Automate VMs with Azure Logic Apps | Auto-Start & Auto-Shutdown Made Easy!

Start the test VM(s) and in the runbook view under your automation account, click the Start button from the top menu. Verify the parameters are correct if you opted not to use the defaults.

Set Simulation to True in order to test without making changes. Verify Run on Azure is selected and click OK.

Open the Output view, and wait for the runbook to execute. It takes a minute or two to queue and run.

The expected output should include messages telling you a tagged VM or resource group was found, that the current time is within a shutdown schedule, and that the intended VMs would have been stopped in a normal execution. Any errors that occur should also be recorded in the output.

Here are the steps to test the opposite case:

  1. Go back to the test VM or resource group and set the AutoShutdownSchedule such that it doesn’t cover the current time. For example, if today is Wednesday, set the tag value to “Tuesday”.
  2. Now start the runbook again using the same steps as before and watch the output

This time, you should see that the current time doesn’t match any shutdown schedules for the VM or group, and see the runbook report that it would have started the intended VMs.

Using Azure CLI and Bicep Template

Credit: youtube.com, Automating your Azure deployment with ARM and Bicep templates

You can also use Azure CLI to manage your auto-shutdown policy. Using the Azure CLI, you can configure the auto-shutdown time and notification settings for your Virtual Machine. This can be done using the `az vm auto-shutdown` command.

To configure the auto-shutdown time, you'll need to specify the time zone and the time at which you want the VM to shut down. You can also configure notification settings, such as sending an email or making an HTTP POST request to a webhook endpoint.

Here are some common options for the `az vm auto-shutdown` command:

Note that you can also use Bicep templates to configure your auto-shutdown policy. Using a Bicep template, you can define the auto-shutdown time and notification settings for your Virtual Machine, and then deploy the template to Azure.

Stop Azure VM with Azure CLI

You can use the Azure CLI to stop and deallocate Azure Virtual Machines (VMs), which is convenient when automation is a better solution than using the Azure Portal.

Credit: youtube.com, Azure - VM Using Azure Cli

The Azure CLI allows you to stop and deallocate Azure VMs with a few simple commands.

To stop an Azure VM using the Azure CLI, you can use the following commands:

  1. Within the Azure Portal, navigate to the Virtual Machine blade for the desired VM.
  2. On the Overview pane, click the Start button.

However, this is not the most efficient way to stop an Azure VM using the Azure CLI.

Unfortunately, the Azure CLI commands provided in the article are not actually used to stop an Azure VM, but rather to start it. To stop an Azure VM using the Azure CLI, you can use the following commands, which are not explicitly stated in the article, but can be inferred from the context.

The reason to stop Azure VMs is to save money by reducing the compute time, which is the largest share of Azure subscription costs when using Virtual Machines (IaaS).

Stopping Azure VMs can be done using the Azure CLI, PowerShell cmdlets, or even the Azure Portal. However, using the Azure CLI is a more efficient solution, especially when automation is required.

Credit: youtube.com, Manage Virtual Machines with the Azure CLI

The article encourages you to check out the "Quickly Start / Stop ALL VMs" article for more description and additional script samples for stopping Azure VMs.

By stopping Azure VMs, you can reduce your Azure subscription costs by minimizing the compute time, which is a significant portion of your bill.

Scheduling and Tags

To manage the times your virtual machines are shut down and powered on, you need a way to define this schedule. A runbook schedule isn't the best approach, as it can get confusing and awkward to manage with multiple schedules for different VMs.

Tag-based Power Schedules are a better alternative. You simply apply a tag to a virtual machine or an Azure resource group that contains VMs. This tag is a simple string that describes the times the VM should be shut down.

The runbook looks for a tag named “AutoShutdownSchedule” assigned to a virtual machine or resource group containing VMs. The value of this tag is one or more schedule entries, or time ranges, defining when VMs should be shut down.

Credit: youtube.com, Scheduling Azure Virtual Machine (VM) Startup With Azure Automation And Tags

There are two kinds of entries: Time range and Day of week / Date. Time range is used to define a period of time when VMs should be shut down, and Day of week / Date is interpreted as a full day that VMs should be shut down.

Here are some examples of schedule tag formats:

The runbook expects all times to be in UTC, not local time. You should convert the times to UTC / GMT before defining a schedule.

Azure Policy and Runbook

Azure Policy and Runbook is a powerful combination for automating shutdown policies on your Azure virtual machines. This is where the Assert-AutoShutdownSchedule runbook comes into play, allowing you to implement granular power schedules for your VMs using simple tag metadata in the Azure portal or through PowerShell.

The runbook can be imported into your Automation Account by uploading the "Assert-AutoShutdownSchedule.ps1" file, which is contained in the download. After importing, you'll need to publish the runbook to make it available for scheduling.

Credit: youtube.com, Azure Policy Remediation Deep Dive

To schedule the runbook, you'll need to create a new schedule in the Azure portal, linking it to your runbook. This will allow the runbook to run periodically, checking the schedule tags on your virtual machines or resource groups every hour. The schedule can be set to run at a specific time, such as the next upcoming hour mark, and can be configured to run hourly.

Here's a quick rundown of the steps to schedule the runbook:

  • Open the runbooks list and select the new runbook "Assert-AutoShutdownSchedule"
  • Open the Schedules view under details
  • Click Add a schedule in the top menu
  • Click Link a schedule to your runbook
  • Click Create a new schedule
  • Provide a name for the schedule, such as "Hourly Runbook Schedule"
  • Set the start time to the desired time
  • Set Recurrence to Hourly
  • Click Create

By following these steps, you'll have the runbook scheduled to run every hour, checking the schedule tags on your virtual machines or resource groups and implementing the power schedules as needed.

Frequently Asked Questions

How does auto-shutdown work?

The auto-shutdown feature shuts down your machine after a set period of inactivity, ranging from 1 hour to 1 week. This ensures your machine automatically powers down when not in use.

Thomas Goodwin

Lead Writer

Thomas Goodwin is a seasoned writer with a passion for exploring the intersection of technology and business. With a keen eye for detail and a knack for simplifying complex concepts, he has established himself as a trusted voice in the tech industry. Thomas's writing portfolio spans a range of topics, including Azure Virtual Desktop and Cloud Computing Costs.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.