Quickstart: Create Advisor alerts on new recommendations by using Bicep

This article shows you how to set up an alert for new recommendations from Azure Advisor by using Bicep.

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.

Whenever Advisor detects a new recommendation for one of your resources, an event is stored in an Azure activity log. You can set up alerts for these events from Advisor by using a recommendation-specific alerts creation experience. You can select a subscription and optionally select a resource group to specify the resources that you want to receive alerts on.

You can also determine the types of recommendations by using these properties:

  • Category
  • Impact level
  • Recommendation type

You can also configure the action that takes place when an alert is triggered by:

  • Selecting an existing action group.
  • Creating a new action group.

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

Note

Advisor alerts are currently only available for High Availability, Performance, and Cost recommendations. Security recommendations aren't supported.

Prerequisites

Review the Bicep file

The Bicep file used in this quickstart is from Azure Quickstart Templates.

@description('Specify the name of alert')
param alertName string

@description('Specify a description of alert')
@allowed([
  'Active'
  'InProgress'
  'Resolved'
])
param status string = 'Active'

@description('Specify the email address where the alerts are sent to.')
param emailAddress string = 'email@example.com'

@description('Specify the email address name where the alerts are sent to.')
param emailName string = 'Example'

resource emailActionGroup 'microsoft.insights/actionGroups@2021-09-01' = {
  name: 'emailActionGroupName'
  location: 'global'
  properties: {
    groupShortName: 'string'
    enabled: true
    emailReceivers: [
      {
        name: emailName
        emailAddress: emailAddress
        useCommonAlertSchema: true
      }
    ]
  }
}

resource alert 'Microsoft.Insights/activityLogAlerts@2020-10-01' = {
  name: alertName
  location: 'global'
  properties: {
    enabled: true
    scopes: [
      subscription().id
    ]
    condition: {
      allOf: [
        {
          field: 'category'
          equals: 'ResourceHealth'
        }
        {
          field: 'status'
          equals: status
        }
      ]
    }
    actions: {
      actionGroups: [
        {
          actionGroupId: emailActionGroup.id
        }
      ]
    }
  }
}

The Bicep file defines two resources:

Deploy the Bicep file

  1. Save the Bicep file as main.bicep to your local computer.

  2. Deploy the Bicep file by using either the Azure CLI or Azure PowerShell.

    az group create --name exampleRG --location eastus
    az deployment group create --resource-group exampleRG --template-file main.bicep --parameters alertName=<alert-name>
    

    Note

    Replace <alert-name> with the name of the alert.

    When the deployment finishes, you should see a message that indicates the deployment succeeded.

Validate the deployment

Use the Azure portal, the Azure CLI, or Azure PowerShell to list the deployed resources in the resource group.

az resource list --resource-group exampleRG

Clean up resources

When you no longer need the resources, use the Azure portal, the Azure CLI, or Azure PowerShell to delete the resource group.

az group delete --name exampleRG