From the Azure Portal, you can easily enable Auto-shutdown on the Virtual Machine Properties page in order to stop your Virtual Machines on a specific schedule, but you can not schedule the Virtual Machines to start. Auto-Shutdown is very interesting to reduce Azure costs for non-production environments.
To be able to automatically start your virtual machine on a specific time you can use the Automation account by creating runbooks, but you can also use Logic Apps if you do not want to use PowerShell.
In this article, I will describe how to start a Virtual Machine using Azure Logic Apps, but you can also use Logic Apps to stop Virtual Machines.
Let’s start by creating a Logic Apps instance. Open the Azure Portal, navigate to “Logic Apps” and click “Add”
Enter the name of your Logic Apps and click “Create”
The Logic Apps designer will appear. First, add the “Recurrence” trigger in order to start this instance on a specific schedule.
You can schedule the instance depending your needs. In this example, I configured the recurrence every day at 07:00 AM:
Next, we add a new step called “HTTP”.
Virtual Machines can be started using the Azure API. The following documentation describes how to start a Virtual Machine:
https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines/start
We must use the HTTP action with the POST method:
Replace the following items:
- Subscription ID
- Resource Group Name
- VM Name
We can enable authentication at this step. Add Authentication type named “Managed Identity”:
Validate the Logic Apps, and go to the “Identity” tab to enable the following setting:
When you will save the configuration, a pop-up will explain that your Logic Apps will be registered with Azure Active Directory. It means that you can grant your Logic Apps to access resources such as Virtual Machines. It is important in our case because the Logic Apps needs to start Virtual Machines.
Navigate to the Virtual Machines blade, select your Virtual Machine, and go to “Access Control (IAM)”. Click “Add” -> “Add role assignment”, then select the “Virtual Machine Contributor” role and link this role to your Logic Apps. You can of course grant this role for all Virtual Machines and not only one VM.
If your Logic Apps is not running, you can click “Run trigger”
Now, you just need to wait until the next schedule of your Logic Apps. The Virtual Machine should be running and you should see in the Activity Log the following output:
In my case, the Virtual Machine has been started and I can confirm that the poweron was initiated by the Logic Apps called “Start-VirtualMachine”
Conclusion
Thanks to the Logic Apps, we can easily perform actions on the Azure resources instead of using Azure Automation. It can be interesting if you do not want to use scripts/runbooks.
Please note that you can of course use Logic Apps to perform actions on resources that are not Virtual Machines. Referring to the following link, you can use API for all the Azure resources: https://docs.microsoft.com/en-us/rest/api/