Adventures with Azure Functions: Create an Alert from App Insights to Send an Email Notification

I’ve been working fiddling with a better way to receive a notification from an Azure Function that has encountered an exception.

There are probably a few approaches to doing this, some that involve code others that do not.

My first choice, is to try to take the no-code or low-code approach.

So to get things going I created the following Azure resources:

  • XXX-rg Resource Group
  • XXX-funcapp Function App
  • XXXstcacc Storage Account
  • XXX-funcapp App Insights

I added a function to my Function App, HttpTrigger1 that contains the following code:

using System;
using System.Net;
using Microsoft.AspNetCore.Mvc;

public static IActionResult Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    throw new NotSupportedException();
}

Navigate to the Function App in the Azure Portal, and click Application Insights on the Overview tab.

Click Alerts and then click New Alert Rule.

Click Add under the Conditions section.

In the filter, type failure and then select HttpTrigger1 Failures.

Configure the Alert Condition with the following settings:

  • Set Operator to Greater than or equal to
  • Set Aggregation type to Count
  • Set Threshold value to 1

Click Done.

Under the Actions section, click Create action group.

Configure the Action Group with the following settings:

  • Set Action group name
  • Set Short name
  • Set Resource Group to the resource group you created
  • Check Email in the Email/SMS/Push/Voice blade
  • Enter an Email address where you want to receive notifications
  • Set the Enable the common alert schema to Yes

Click OK.

Navigate back to the HttpTrigger1 and click Run a couple of times to generate some exceptions.

In 2 to 3 minutes, you should receive an email that the alert has fired.

In about another 2 to 3 minutes you should receive another email letting you know the error has been resolved.

This is a little deceptive, assuming the code is what caused the exception, which in our case it is, the alert is not “really” resolved, it just means it hasn’t fired in a minute or so.

What if you wanted to receive an email notification immediately? According to the documentation, it is not possible to reduce the latency on when alerts are fired.

So is there another way? Stay tuned.

Related Articles

Leave a Reply

Your email address will not be published.