Shutting down azure functions app if cost threshold is passed

83 Views Asked by At

I am trying to set up a mechanism to shut down my Azure Functions app if a cost alert gets thrown. I have created an automation account, a powershell runbook, and an action group. I went into Cost + Billing in for my tenant in azure and created a Cost Alert. I tried to associate the alert with an Action Group, but I couldn't find a way. I don't know if this was ever possible in the interface. Next I tried another way: I started setting up a Logic App to kick off when I receive the alert email but there is no "Free" cost plan. Is there a way to stop my app if a price threshold is met for free?

2

There are 2 best solutions below

0
afriedman111 On BEST ANSWER

The "Manage Action Groups" link was not displayed becuase the context I was looking at in Billing was an account and not my subscription. When I changed to my subscription the link appeared on the "Set Alerts" tab.

0
SiddheshDesai On

Create an Azure Cost Budget and filter the Scope with your Function Apps like below:-

enter image description here

Select the Creation and Expiration date for the budget:-

enter image description here

In the Alert condition I selected 80% which means when the cost reaches 80% of my Budget Amount, I will receive an alert and Action group will be triggered:-

enter image description here

Select Manage action group> Create a new Action Group and select any of the method like Automation account or Logic App to stop your function app:-

enter image description here

In the Notification Enter your email to get notified for this Budget:-

enter image description here

Create Action and select the Automation Runbook to stop the Function app once the Alert is triggered:-

enter image description here

Select > Next > Tags > Review + create > Create the Budget.

I have created one Azure Automation account> Created Powershell script to stop the Function Apps from my Subscription like below:-

My Automation runbook script:-

$SecurePassword = ConvertTo-SecureString -String "xxxxx2gn0pRTx.Lv3Ockk" -AsPlainText -Force
$TenantId = 'xxxxx-af9038592395'
$ApplicationId = 'xxxxx26a31435cb'
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationId, $SecurePassword
Connect-AzAccount -ServicePrincipal -TenantId $TenantId -Credential $Credential

# Get all Function Apps in the current subscription
$functionApps = Get-AzFunctionApp

# Stop each Function App
foreach ($app in $functionApps) {
    $appName = $app.Name
    $resourceGroupName = $app.ResourceGroup

    Write-Host "Stopping Function App: $appName in Resource Group: $resourceGroupName"

    Stop-AzFunctionApp -ResourceGroupName $resourceGroupName -Name $appName -Force
}

Write-Host "All Function Apps stopped successfully."

enter image description here

Output:-

enter image description here