how to stop active resources using code when costs exceed budget

Winnie Yap 40 Reputation points
2025-05-08T09:12:42.64+00:00

Hi Everyone:

A quick question related to this post of mine https://learn.microsoft.com/en-us/answers/questions/2261863/how-to-stop-databricks-using-powershell-code

Is it possible to stop all active resources (also non databricks resources) when costs exceed a certain threshold? I'm looking for a way to do this using code but I am not 100% sure how to do this. Is this even possible to do using runbooks or something like that? Any advice is highly appreciated!

Hope to hear from you guys,

Winnie

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
3,366 questions
{count} votes

Accepted answer
  1. Ashok Gandhi Kotnana 7,160 Reputation points Microsoft External Staff Moderator
    2025-05-08T11:05:38.5633333+00:00

    Hi @Winnie Yap ,

    Yes, it is possible to stop or deallocate resources in Azure when a cost threshold is exceeded, but there are important caveats and limitations depending on the resource type. Here's a structured breakdown of how you can achieve this:

     Overall Strategy Create a Budget Alert in Azure Cost Management.

     Trigger an Action Group on budget threshold breach.

     Use Automation (Runbook, Logic App, or Azure Function) as the action group target.

     Runbook stops IaaS resources (like VMs, SQL VMs).

     PAAS & SAAS services limitations (some cannot be "stopped"—only deleted or scaled down).

     step-by-Step Implementation Guide

     1. Create a Budget with Alert Go to Cost Management + Billing > Budgets > Create a new budget at the Subscription level.

     Set a threshold (e.g., 80%) and define Action Group to be triggered on that threshold.

     2. Action Group Setup In Azure Monitor > Alerts > Action groups, create a new group.

     Add an Azure Automation Runbook (or Logic App) as the action.

     3. Automation Runbook Create a PowerShell Runbook in an Azure Automation Account. Example code:

    $vmList = Get-AzVM 
    foreach ($vm in $vmList) {     
    if ($vm.PowerState -ne "VM deallocated") {  
           Stop-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Force 
        } 
    }
    

     Limitations App Services (PaaS): Cannot be "stopped" fully—only scaled to the lowest tier.

     Office 365 / SaaS: Cannot be programmatically stopped—require licensing changes or manual removal.

     Stopping the entire Subscription is possible but risky—Azure will mark the subscription as disabled (read-only), and permanent deletion happens in 90 days.

    Recommendations If you only want to prevent excessive cost, stopping or deallocating major IaaS resources is usually enough.

     For broader coverage, you could:

     Tag critical resources and only stop those.

     Use Logic Apps or Azure Functions for more complex conditions

    References:

    https://docs.azure.cn/en-us/cost-management-billing/manage/subscription-states https://docs.azure.cn/en-us/cost-management-billing/manage/subscription-disabled

    Please let me know if you face any challenge here, I can help you to resolve this issue further

    Provide your valuable Comments.

    User's image

    Please do not forget to "Accept the answer” and “upvote it” wherever the information provided helps you, this can be beneficial to other community members.it would be greatly appreciated and helpful to others.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.