Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use Azure Functions to set up custom triggers for VM shutdowns


Can I use Azure Functions to set up custom triggers for VM shutdowns


Yes, you can use Azure Functions to set up custom triggers for VM shutdowns. Here's how you can approach this:

Using Azure Functions for Custom VM Shutdown Triggers

1. Create an Azure Function App

- Start by creating an Azure Function App in the Azure Portal.
- Choose a language like PowerShell or Python for your function, depending on your preference and requirements.
- Use an HTTP Trigger or a Timer Trigger depending on whether you want to trigger the shutdown via an HTTP request or at a scheduled time.

2. Configure the Function

- In your Azure Function, use Azure PowerShell commands to manage VMs. For example, you can use `Stop-AzVM` to shut down a VM.
- Ensure your Function App has a managed identity with the appropriate permissions to manage VMs in your subscription.

3. Implement Custom Logic

- You can implement custom logic within your function to decide when to shut down VMs based on specific conditions, such as CPU usage, time of day, or external events.

4. Triggering the Function

- Use HTTP requests to trigger the function from external sources, such as web applications or scripts running on VMs.
- Alternatively, use a Timer Trigger to schedule shutdowns at specific times or intervals.

Example PowerShell Code for VM Shutdown

powershell
using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Get the VM name from the request body
$vmName = $Request.Body.Name

# Stop the VM
Stop-AzVM -Name $vmName -ResourceGroupName "YourResourceGroupName" -Confirm:$false -NoWait -Force

# Return a response
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = "VM $vmName has been stopped."
})

Additional Considerations

- Email Notifications: You can integrate services like SendGrid or Azure Communication Services to send email notifications when VMs are shut down.
- Permissions: Ensure your Function App's managed identity has the necessary permissions to manage VMs in your Azure subscription.

By following these steps, you can create a flexible and customizable solution for shutting down VMs using Azure Functions.

Citations:
[1] https://www.wesleyhaakman.org/azure-functions-ifttt-alexa/
[2] https://www.reddit.com/r/AZURE/comments/1ana141/azure_function_app_schedule_startstop_vm/
[3] https://charbelnemnom.com/schedule-azure-vm-using-azure-function/
[4] https://avdpunks.com/2023/01/12/avd-shutdown-and-deallocate-session-host-at-logoff/
[5] https://techcommunity.microsoft.com/blog/startupsatmicrosoftblog/how-to-automatically-shut-down-idle-vms-in-azure/4376055
[6] https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings
[7] https://learn.microsoft.com/en-us/azure/azure-functions/start-stop-vms/overview
[8] https://github.com/Azure/azure-functions-host/issues/2746