Search code examples
azureazure-functionsazure-storageazure-virtual-machine

What is the most cost-effective way to automatically start an Azure VM?


I am an Azure rookie. Currently, I have a small cheap VM that is always running, from which I start a big expensive VM via command line (i.e. a simple az vm start -g foo -n bar). However, I am wondering if there is a more elegant way of doing this. Do you have any suggestions?

Important: it should not cost more than 5$/month. My current setup is 'free' because I need the small cheap VM to be running anyway.

I tried the Microsoft's Start/Stop VMs v2 function, which is recommended by Azure. Before deploying it, I tried to figure out the projected cost of this solution. Here is a summary of the components and their prices.

Looks great, a neat and elegant solution for 5$/month, exactly what I need, right? BUT: after deploying the function and monitoring the costs, it turns out that the Storage account is by default protected by the Microsoft Defender for Cloud, which was not mentioned anywhere before. This one alone costs over 10$/month. The security layer is of course important. I don't want to sound cheap but those costs accumulate over time and will increase further when scaling up or out.

Thus, back to my question: What would you suggest as the best practice to automatically start/stop a virtual machine in Azure?

Maybe there is another elegant solution that doesn't require navigating through Microsoft's pricing maze.


Solution

  • You can use Azure Automation to automate the Azure resources using runbooks and create a PowerShell runbooks to start and stop your VM on a schedule by following below steps.

    1. Create a Automation account with System Identity

    2. Create a Runbook in automation account by navigating to Runbooks > Create a runbook

    enter image description here

    1. Open Runbook > Edit in Portal > add below script and click on Publish

    You can find the automation account object ID here.

    enter image description here

    Note: The Identity must have the Virtual Machine Contributor role assigned to start/Stop the VM

     az login --identity --username "Automation Identity object ID"
     az vm start --name "Venkat-VM" --no-wait --resource-group "VM_RG_Name"
    
    1. Schedules the runbook > add a schedule > link schedule to your run book > Create.

    enter image description here

    The VM has been automatically started according to the scheduled time.

    enter image description here

    1. To stop a virtual machine, you can use the default Auto-shutdown option in the VM settings.

    enter image description here