In our CAF Landing Zone Starter Kit, the implementation of the Anomaly Alert on the subscription was missing until now. This is almost complete. Here is a possible implementation of the Anomaly Alert as a Bicep module.
Anomaly Alert Bicep
targetScope = 'subscription'
@description('Name of the Anomaly Alert')
param aaName string = 'myAnomalyAlert'
@description('Display Name of the Anomaly Alert')
param aaDisplayName string = 'Anomaly Alert'
@description('Subject of the Anomaly Alert email notification')
param aaNotificationSubject string = 'Anomaly Alert detected for your Subscription'
@description('Email address to send the Anomaly Alert notification to')
param aaNotificationTo string
@description('Message of the Anomaly Alert email notification')
param aaNotificationMessage string = 'The Anomaly Alert has been triggered for your Subscription ${subscription().subscriptionId}. Please check the Cost Management Dashboard for more details.'
@description('Start date of the Anomaly Alert (default: now)')
param aaStartDate string = utcNow('u')
@description('End date of the Anomaly Alert (default: 1 year from now)')
param aaEndDate string = dateTimeAdd(utcNow('u'), 'P1Y')
// The kind of the Anomaly Alert must be 'InsightAlert'
var aaKind = 'InsightAlert'
resource myAnomalyAlert 'Microsoft.CostManagement/scheduledActions@2022-10-01' = {
name: aaName
kind: aaKind
properties: {
displayName: aaDisplayName
scope: '/subscriptions/${subscription().subscriptionId}'
notification: {
subject: aaNotificationSubject
to: [
aaNotificationTo
]
message: aaNotificationMessage
}
status: 'Enabled'
viewId: resourceId('Microsoft.CostManagement/views/','ms:DailyAnomalyByResourceGroup')
schedule: {
endDate: aaEndDate
frequency: 'Daily'
startDate: aaStartDate
}
}
}
Deploying the Module
The module can be applied using the Azure CLI. The important thing is to verify that you are in the correct subscription first. Then the deployment is straightforward:
az deployment sub create --name aaDeplyoment01 --location westeurope --template-file .\main.bicep
The deployment will then prompt for the email address and set up the Anomaly Alert.
