Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I integrate Azure Functions with IFTTT for VM shutdowns


How can I integrate Azure Functions with IFTTT for VM shutdowns


Integrating Azure Functions with IFTTT for shutting down virtual machines (VMs) involves several steps. Here's a step-by-step guide to achieve this integration:

1. Create an Azure Function

First, you need to create an Azure Function that can shut down VMs. This function will be triggered by an HTTP request.

- Create the Function: Use the Azure Functions Core Tools to create a new function. Choose PowerShell as the language and HTTPTrigger as the trigger type.
- Function Logic: In the function, use Azure PowerShell commands to list and stop VMs. For example, you can use `Get-AzVM` to list VMs and `Stop-AzVM` to stop them. Ensure you configure Managed Identity with "Virtual Machine Contributor" permissions.

powershell
# Example PowerShell code to stop VMs
$vms = Get-AzVM -ResourceGroupName $rgName
foreach ($vm in $vms) {
    if ($vm.PowerState -eq 'VM running') {
        Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force -NoWait
    }
}

2. Deploy and Configure the Function

- Deploy the Function: Deploy your Azure Function to Azure. You can use Visual Studio Code for this purpose.
- Configure Managed Identity: Ensure Managed Identity is configured for your Function App with the appropriate permissions to manage VMs.

3. Set Up IFTTT

1. Create an IFTTT Account: If you haven't already, create an account on IFTTT.com.
2. Create a New Applet: Go to the "Create" section and start a new applet.
3. Configure the Trigger: Search for "Alexa" and set up a trigger based on a specific phrase, such as "turn off virtual machines."
4. Configure the Action: Search for "Webhooks" and add it as the action. Paste the URL of your Azure Function into the URL field. Set the method to "GET" (or "POST" if you need to pass parameters) and the content type to "application/json."

4. Integrate with Alexa

- Connect IFTTT to Alexa: During the IFTTT setup, you will be prompted to connect IFTTT to Alexa. Enter your credentials and allow access.
- Create an Alexa Routine: In the Alexa app, create a new routine that triggers the IFTTT applet you just created.

5. Test the Setup

- Trigger the Command: Use Alexa to trigger the command, such as "Alexa, trigger turn off virtual machines."
- Verify the Result: Check the Azure portal to ensure the VMs are shutting down as expected.

This setup allows you to use IFTTT as a bridge between Alexa and Azure Functions, enabling voice control over your Azure resources without needing to develop a custom Alexa skill.

Citations:
[1] https://www.wesleyhaakman.org/azure-functions-ifttt-alexa/
[2] https://datasavvy.me/2019/08/15/using-azure-automation-to-shut-down-a-vm-only-if-a-sql-agent-job-is-not-running/
[3] https://www.dcac.com/2025/01/13/calling-azure-automation-using-ifttt/
[4] https://stackoverflow.com/questions/37204782/is-it-possible-to-stop-start-an-azure-arm-virtual-machine-from-an-azure-function
[5] https://www.infoworld.com/article/2265048/azure-logics-apps-the-ifttt-or-quick-basic-for-the-serverless-era.html
[6] https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings
[7] https://www.aaron-powell.com/posts/2017-11-17-simple-apis-with-flow-and-azure-functions/
[8] https://xenithit.blogspot.com