Reacting to Azure Alerts with Azure Functions

Posted by

Alerts can be configured on almost any resource in Azure. The alert conditions vary from one resource to another, but in all cases, once triggered, the alert performs the actions you specify. One of those actions can be to call an Azure Function, opening up a bunch of automation options.

I’ll cover the steps to configure an alert that invokes a function, and then describe some scenarios where doing so could be useful.

Getting It Working

Calling a function from an alert is fairly straightfoward, but not well documented. What added to my confusion was that the Azure portal let me choose any function trigger, be it a timer, queue, or other, to handle the alert.

Luckily, I found a post by Billy York that explains the process with a PowerShell trigger function. His post uses an HTTP Trigger, so I set off to do the same. I suspect that any function trigger that receives a string object works too, but I haven’t tested it.

To summarize, here’s what you’ll need to do:

  • Create an HTTP Trigger function in the language of your choosing.
  • Write your custom code to handle the alert.
  • Deploy the function to Azure.
  • Create an alert, choose Azure Functions for the Action type, and then select the function you just deployed.
  • Use the Common Alerting Schema. It’ll be easier to handle any type of alert message that the function receives.

Effectively, the function acts as a webhook that’s invoked every time the alert fires. It’s up to you to decide what to do when the webhook is called.

Potential Use Cases

The alerts in Azure can be broken up into three groups:

  • Technical metrics: they tell you the state of a resource, such as the number of errors, number of connections, incoming requests, outgoing requests, total execution time, etc.
  • Activity metrics: they are triggered any time a change is made to a resource, like creating, updating, or deleting a resource.
  • Custom metrics: they are created from custom queries against the data you collect in Application Insights.

These metrics can be leveraged to eliminate some of the maintenance burden of your applications:

  • Restarting a service or container if it stops processing messages from a Service Bus, Event Hub, or Event Grid.
  • Automatically resize Azure SQL databases when disk usage goes past a certain percentage.
  • Log activity metrics to a central data store for auditing purposes.
  • Detect infrastructure-level errors and launch corrective processes.
  • Contact impacted customers when an application exceeds its response time SLA.

Of course, there are tools on the market that monitor many of these metrics for you. For smaller cloud projects, it might make more sense to start with a custom solution, and move to a SaaS like DataDog when the need presents itself.

Alerts make it easy to react to infrastructure events in Azure. You may not need them that often, but it’s good to know they are there for those rare occasions.


Did you find the information you were looking for? Is there something missing from this article that would make it more useful to you? Do you have a follow-up question? Feel free to reach out to me on Twitter or LinkedIn. I’d be glad to help out.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s