This azure function is used to start a VM if it is not running, and send a notification to gotify if it is running,
This is used to start a spot VM that is stopped due to low capacity
This is triggered by a webhook, But can be triggered by a timer or queue if needed.
- Use run_http_trigger.ps1 for a webhook trigger, and run_timer_trigger.ps1 for a timer trigger
- This is a simple example, and can be expanded to do more things, like starting multiple VMs, or sending notifications to multiple services, or even starting a VM on a schedule.
Click Create a resource and select Function App. Give it a nice name, such as az-vm-start-999.
Runtime stack means what language we would like to write the Function logic. Azure Function provides support for mainstream languages or platforms such as .NET, Java, Node.js, Python, and PowerShell Core. Here I will choose PowerShell Core. Select Consumption (Serverless) for Plan type, this Plan can be billed dynamically according to your usage, which helps save money when the usage is small.
Next, we have to create or select a Storage Account, which is required by the latest version of Azure Function V3, u can leave this as default
Click Review + create to create the Function App.
After the creation is completed, we need to configure permissions for this Function App to access VM.
Under the Identity menu, turn on the System assigned switch. This authorization method allows Azure resources to authorize each other within a few mouse click.
Now go to your VM management screen, go to Access control (IAM) and select Add > Role Assignment
In the Role tab, Search and Select Virtual Machine Contributor role, Then click next
Go to Member and after selecting Managed Identity select the function app you created, and select your subscription, then select the function app you created
And you are Done setting up permission, now u can edit the run.ps1 file in the function app with your VM name, resource group name, Subscription ID, and tenantId and save/copy it to the function app.
Here we have sevaral option to setup the function app, we can use a timer, a webhook, or a queue, in this example we will use a timer, but you can use a webhook if you want to trigger it from outside azure. A timer is the easiest way to trigger it, and you can set it to run every 5 minutes or so(High consumption of the function app will cost you more money), and it will check if the VM is running, if it is not running it will start it, and if it is running it will send a notification to gotify.
Go to the created Function App, and create a new Function, select timer trigger or Http trigger, and give it a name, and set the schedule to your liking, and click create
And lastly go to code + test, and paste the code from either from run_http_trigger.ps1 or from run_timer_trigger.ps1 depending on your function app, and save it, and you are done, you can test it by clicking run, and you can see the logs in the log tab.