Scheduling the Uptime of an Azure Virtual Machine

In a previous post I setup a build agent in a private pipeline - hosted within a Azure Virtual Machine (VM). In this one, I try to minimize the uptime of the VM by making it run only during work hours - I require it to start automatically first thing in the morning and then turn itself off at the close of business. In order to achieve this I make use of the Azure Automation feature.

  1. Let's start off by logging into our Azure account and creating a new Automation Account entry.

    Search Automation Account

    Create Automation Account

  2. Provide the required details:

    • Name - Name for the automation account.
    • Subscription - Azure subscription for this account.
    • Resource Group - Create or select an existing resource group from the list.
    • Location - Select an available region from the list.
    • Create Azure Run As account - Select Yes.

    Add Automation Account

  3. Click the Create button to start provisioning the automation account.

  4. Once done, navigate to it's blade via the resource groups and click on the Runbooks widget.

    Create runbook

  5. The runbook gallery had a couple of really good graphical runbooks that suited my purposes - the Start Azure V2 VMs and Stop Azure V2 VMs.

    Click on the Browse Gallery button, select the Start Azure V2 VMs graphical runbook and then click OK to confirm the selection.

    Choose Graphical Runbook

  6. Inspect the flow and click the Import button when ready.

    Import Graphical Runbook

  7. Navigate into the newly added runbook and click the Edit button. Make any alterations if required and click the Publish button.

    Publish Runbook

  8. Back on the runbook blade, click on Schedules widget and then click on the Add a schedule button.

    Add Schedule

  9. Click on the Link a schedule to your runbook and then create a new schedule. Provide the time and recurrence for the script execution. Read more about scheduling here.

    Finalize Schedule

  10. Next, select Configure parameters and run settings and provide a virtual machine name or resource group which would start on execution.

    Schedule parameters

  11. Click the ok button once done. You should get a confirmation that the schedule is linked to a runbook.

    That's it. The runbook would execute at the pre-defined time.

  12. Repeat the above steps again to create a stop script using the Stop Azure V2 VMs graphical runbook.

References

comments powered by Disqus