This construct provides a type-safe, version-aware implementation of Azure Activity Log Alerts using the AZAPI provider framework.
Activity Log Alerts monitor Azure Activity Log events and trigger notifications when specific operations occur, such as resource deletions, configuration changes, or service health events.
- Version Management: Automatic resolution to the latest stable API version (2020-10-01)
- Type Safety: Full TypeScript type definitions with JSII compliance
- Comprehensive Filtering: Filter by category, operation, resource type, status, and more
- Service Health Alerts: Monitor Azure service health events
- Resource Health Alerts: Monitor resource-specific health events
- Validation: Schema-driven property validation
- Multi-language: Generated bindings for TypeScript, Python, Java, and C#
This construct is part of the @microsoft/terraform-cdk-constructs package.
npm install @microsoft/terraform-cdk-constructsimport { ActivityLogAlert } from "@microsoft/terraform-cdk-constructs/azure-activitylogalert";
import { ActionGroup } from "@microsoft/terraform-cdk-constructs/azure-actiongroup";
const vmDeletionAlert = new ActivityLogAlert(this, "vm-deletion", {
name: "vm-deletion-alert",
description: "Alert when any VM is deleted",
resourceGroupId: resourceGroup.id,
scopes: ["/subscriptions/00000000-0000-0000-0000-000000000000"],
condition: {
allOf: [
{ field: "category", equals: "Administrative" },
{ field: "operationName", equals: "Microsoft.Compute/virtualMachines/delete" },
{ field: "status", equals: "Succeeded" }
]
},
actions: {
actionGroups: [{
actionGroupId: actionGroup.id
}]
},
tags: {
Environment: "Production",
AlertType: "Security"
}
});const serviceHealthAlert = new ActivityLogAlert(this, "service-health", {
name: "service-health-alert",
description: "Alert on Azure service health incidents",
resourceGroupId: resourceGroup.id,
scopes: ["/subscriptions/00000000-0000-0000-0000-000000000000"],
condition: {
allOf: [
{ field: "category", equals: "ServiceHealth" },
{ field: "properties.incidentType", equals: "Incident" }
]
},
actions: {
actionGroups: [{
actionGroupId: actionGroup.id,
webhookProperties: {
severity: "critical",
environment: "production"
}
}]
}
});const resourceHealthAlert = new ActivityLogAlert(this, "resource-health", {
name: "resource-health-alert",
description: "Alert on resource health degradation",
resourceGroupId: resourceGroup.id,
scopes: [resourceGroup.id],
condition: {
allOf: [
{ field: "category", equals: "ResourceHealth" },
{ field: "properties.currentHealthStatus", equals: "Degraded" },
{ field: "resourceType", equals: "Microsoft.Compute/virtualMachines" }
]
},
actions: {
actionGroups: [{
actionGroupId: actionGroup.id
}]
}
});const securityAlert = new ActivityLogAlert(this, "security-alert", {
name: "security-center-alert",
description: "Alert on new Security Center recommendations",
resourceGroupId: resourceGroup.id,
scopes: ["/subscriptions/00000000-0000-0000-0000-000000000000"],
condition: {
allOf: [
{ field: "category", equals: "Security" },
{ field: "operationName", equals: "Microsoft.Security/locations/alerts/activate/action" }
]
},
actions: {
actionGroups: [{
actionGroupId: securityActionGroup.id
}]
}
});const policyAlert = new ActivityLogAlert(this, "policy-violation", {
name: "policy-violation-alert",
description: "Alert on Azure Policy violations",
resourceGroupId: resourceGroup.id,
scopes: ["/subscriptions/00000000-0000-0000-0000-000000000000"],
condition: {
allOf: [
{ field: "category", equals: "Policy" },
{ field: "operationName", equals: "Microsoft.Authorization/policies/audit/action" }
]
},
actions: {
actionGroups: [{
actionGroupId: actionGroup.id
}]
}
});| Property | Type | Description |
|---|---|---|
name |
string |
The name of the activity log alert |
scopes |
string[] |
Resource IDs that this alert is scoped to |
condition |
ActivityLogAlertCondition |
Alert condition with field-value pairs |
| Property | Type | Description | Default |
|---|---|---|---|
location |
string |
Azure region | "global" |
description |
string |
Description of the alert rule | - |
enabled |
boolean |
Whether the alert rule is enabled | true |
actions |
object |
Action groups to notify | - |
tags |
Record<string, string> |
Resource tags | {} |
resourceGroupId |
string |
Resource group ID | Required |
apiVersion |
string |
Explicit API version | Latest |
interface ActivityLogAlertCondition {
allOf: ActivityLogAlertLeafCondition[]; // All conditions are ANDed
}
interface ActivityLogAlertLeafCondition {
field: string; // Field name to filter on
equals: string; // Value to match
}| Field | Description | Common Values |
|---|---|---|
category |
Event category | Administrative, ServiceHealth, ResourceHealth, Alert, Policy, Security |
operationName |
Operation performed | Microsoft.Compute/virtualMachines/delete, etc. |
resourceType |
Type of resource | Microsoft.Compute/virtualMachines, etc. |
resourceGroup |
Resource group name | Name of the resource group |
status |
Operation status | Succeeded, Failed, Started |
subStatus |
Detailed status | Detailed status information |
resourceId |
Full resource ID | Complete Azure resource ID |
| Field | Description | Values |
|---|---|---|
properties.incidentType |
Type of incident | Incident, Maintenance, Information, ActionRequired |
properties.impactedServices[*].ServiceName |
Affected service | Virtual Machines, App Service, etc. |
properties.impactedServices[*].ImpactedRegions[*].RegionName |
Affected region | eastus, westus, etc. |
| Field | Description | Values |
|---|---|---|
properties.currentHealthStatus |
Current status | Available, Degraded, Unavailable |
properties.previousHealthStatus |
Previous status | Available, Degraded, Unavailable |
properties.cause |
Cause of change | PlatformInitiated, UserInitiated |
Tracks create, update, delete, and action operations on resources.
{ field: "category", equals: "Administrative" }Tracks Azure service health events including incidents and maintenance.
{ field: "category", equals: "ServiceHealth" }Tracks health status changes of individual resources.
{ field: "category", equals: "ResourceHealth" }Tracks firing of Azure Monitor alerts.
{ field: "category", equals: "Alert" }Tracks Azure Policy evaluation results.
{ field: "category", equals: "Policy" }Tracks Azure Security Center alerts and recommendations.
{ field: "category", equals: "Security" }Microsoft.Compute/virtualMachines/write- Create or updateMicrosoft.Compute/virtualMachines/delete- DeleteMicrosoft.Compute/virtualMachines/start/action- StartMicrosoft.Compute/virtualMachines/powerOff/action- Power offMicrosoft.Compute/virtualMachines/restart/action- Restart
Microsoft.Storage/storageAccounts/write- Create or updateMicrosoft.Storage/storageAccounts/delete- DeleteMicrosoft.Storage/storageAccounts/regeneratekey/action- Regenerate key
Microsoft.Network/networkSecurityGroups/write- Create or updateMicrosoft.Network/networkSecurityGroups/delete- DeleteMicrosoft.Network/networkSecurityGroups/securityRules/write- Update rules
Activity Log Alerts can be scoped to:
scopes: ["/subscriptions/00000000-0000-0000-0000-000000000000"]scopes: ["/subscriptions/.../resourceGroups/my-resource-group"]scopes: ["/subscriptions/.../resourceGroups/.../providers/Microsoft.Compute/virtualMachines/myVM"]scopes: [
"/subscriptions/.../resourceGroups/rg1",
"/subscriptions/.../resourceGroups/rg2"
]- Use Specific Scopes: Limit alerts to relevant resources to reduce noise
- Add Descriptions: Document what the alert monitors and expected actions
- Filter by Status: Include
statusfield to only alert on succeeded/failed operations - Service Health: Create dedicated alerts for service health events
- Security Monitoring: Alert on critical security operations (deletions, key regeneration)
- Tag Alerts: Use tags to organize and categorize alerts
- Test Actions: Verify action groups receive notifications
- Review Regularly: Analyze activity logs to identify new operations to monitor
The Activity Log Alert construct provides the following outputs:
id: The resource ID of the activity log alertname: The name of the activity log alert
- 2020-10-01 (Active, Latest) - Stable API version with full feature support
See the examples directory for complete working examples:
- Resource operation monitoring
- Service health alerting
- Resource health monitoring
- Security event alerting
- Policy compliance monitoring
ActionGroup- Notification hubMetricAlert- Metric-based alerting