Quickstart: Create activity log alerts on service notifications using a Bicep file

This article shows you how to set up activity log alerts for service health notifications by using a Bicep file.

Bicep is a domain-specific language (DSL) that uses declarative syntax to deploy Azure resources. It provides concise syntax, reliable type safety, and support for code reuse. Bicep offers the best authoring experience for your infrastructure-as-code solutions in Azure.

Service health notifications are stored in the Azure activity log. Given the possibly large volume of information stored in the activity log, there is a separate user interface to make it easier to view and set up alerts on service health notifications.

You can receive an alert when Azure sends service health notifications to your Azure subscription. You can configure the alert based on:

  • The class of service health notification (Service issues, Planned maintenance, Health advisories).
  • The subscription affected.
  • The service(s) affected.
  • The region(s) affected.

Note

Service health notifications does not send an alert regarding resource health events.

You also can configure who the alert should be sent to:

  • Select an existing action group.
  • Create a new action group (that can be used for future alerts).

To learn more about action groups, see Create and manage action groups.

Prerequisites

Review the Bicep file

The following Bicep file creates an action group with an email target and enables all service health notifications for the target subscription. Save this Bicep as CreateServiceHealthAlert.bicep.

param actionGroups_name string = 'SubHealth'
param activityLogAlerts_name string = 'ServiceHealthActivityLogAlert'
param emailAddress string

var alertScope = '/subscriptions/${subscription().subscriptionId}'

resource actionGroups_name_resource 'microsoft.insights/actionGroups@2019-06-01' = {
  name: actionGroups_name
  location: 'Global'
  properties: {
    groupShortName: actionGroups_name
    enabled: true
    emailReceivers: [
      {
        name: actionGroups_name
        emailAddress: emailAddress
      }
    ]
    smsReceivers: []
    webhookReceivers: []
  }
}

resource activityLogAlerts_name_resource 'microsoft.insights/activityLogAlerts@2017-04-01' = {
  name: activityLogAlerts_name
  location: 'Global'
  properties: {
    scopes: [
      alertScope
    ]
    condition: {
      allOf: [
        {
          field: 'category'
          equals: 'ServiceHealth'
        }
        {
          field: 'properties.incidentType'
          equals: 'Incident'
        }
      ]
    }
    actions: {
      actionGroups: [
        {
          actionGroupId: actionGroups_name_resource.id
          webhookProperties: {}
        }
      ]
    }
    enabled: true
  }
}

The Bicep file defines two resources:

Deploy the Bicep file

Deploy the Bicep file using Azure CLI and Azure PowerShell. Replace the sample values for Resource Group and emailAddress with appropriate values for your environment.

az login
az deployment group create --name CreateServiceHealthAlert --resource-group my-resource-group --template-file CreateServiceHealthAlert.bicep --parameters emailAddress='user@contoso.com'

Validate the deployment

Verify that the workspace has been created using one of the following commands. Replace the sample values for Resource Group with the value you used above.

az monitor activity-log alert show --resource-group my-resource-group --name ServiceHealthActivityLogAlert

Clean up resources

If you plan to continue working with subsequent quickstarts and tutorials, you might want to leave these resources in place. When no longer needed, delete the resource group, which deletes the alert rule and the related resources. To delete the resource group by using Azure CLI or Azure PowerShell

az group delete --name my-resource-group

Next steps