This article describes on how to create pre and post events in Azure Update Manager.
Event Grid in schedule maintenance configurations
Azure Update Manager leverages Event Grid to create and manage pre and post events. For more information, go through the overview of Event Grid. To trigger an event either before or after a schedule maintenance window, you require the following:
Schedule maintenance configuration - You can create Pre and post events for a schedule maintenance configuration in Azure Update Manager. For more information, see schedule updates using maintenance configurations.
Action to be performed in the pre or post event - You can use the Event handlers (Endpoints) supported by Event Grid to define actions or tasks. Here are examples on how to create Azure Automation Runbooks via Webhooks and Azure Functions. Within these Event handlers/Endpoints, you must define the actions that should be performed as part of pre and post events.
Pre and post event - You can follow the steps shared in the following section to create a pre and post event for schedule maintenance configuration. To learn more about the terms used in the Basics tab of Event Grid, see Event Grid terms.
Create a pre and post event
Create pre and post events while creating a new schedule maintenance configuration
Sign in to the Azure portal and go to Azure Update Manager.
Under Manage, select Machines.
Select Schedule updates from the ribbon at the top.
In the Create a maintenance configuration page, select the Events tab.
Select +Event Subscription to create pre/post event.
On the Add Event Subscription page, enter the following details:
In the Event Subscription Details section, provide an appropriate name.
Keep the schema as Event Grid Schema.
Enter the System Topic Name for the first event you create in this maintenance configuration. The same System Topic name will be auto populated for the consequent events.
In the Event Types section, Filter to Event Types, select the event types that you want to get pushed to the endpoint or destination. You can select either Pre Maintenance Event or Post Maintenance Event or both. To learn more about event types that are specific to schedule maintenance configurations, see Azure Event Types.
In the Endpoint details section, select the endpoint where you want to receive the response from.
Select Add to create the pre and post events for the schedule upon its creation.
Note
In the above flow, Webhook and Azure Functions are the two Event handlers/endpoints you can choose from. When you select Add, the event subscription is not created but added to the maintenance configuration. Event subscription is created along with the schedule maintenance configuration.
Create a maintenance configuration by following the steps listed here.
# Obtain the Maintenance Configuration ID from Step 1 and assign it to MaintenanceConfigurationResourceId variable
$MaintenanceConfigurationResourceId = "/subscriptions/<subId>/resourceGroups/<Resource group>/providers/Microsoft.Maintenance/maintenanceConfigurations/<Maintenance configuration Name>"
# Use the same Resource Group that you used to create maintenance configuration in Step 1
$ResourceGroupForSystemTopic = "<Resource Group for System Topic>"
$SystemTopicName = "<System topic name>"
$TopicType = "Microsoft.Maintenance.MaintenanceConfigurations"
$SystemTopicLocation = "<System topic location>"
# System topic creation
New-AzEventGridSystemTopic -ResourceGroupName $ResourceGroupForSystemTopic -Name $SystemTopicName -Source $MaintenanceConfigurationResourceId -TopicType $TopicType -Location $SystemTopicLocation
# Event subscription creation
$IncludedEventTypes = @("Microsoft.Maintenance.PreMaintenanceEvent")
# Webhook
$EventSubscriptionName = "PreEventWebhook"
$PreEventWebhookEndpoint = "<Webhook URL>"
$dest = New-AzEventGridWebHookEventSubscriptionDestinationObject -EndpointUrl $PreEventWebhookEndpoint
New-AzEventGridSystemTopicEventSubscription -ResourceGroupName $ResourceGroupForSystemTopic -SystemTopicName $SystemTopicName -EventSubscriptionName $EventSubscriptionName -Endpoint $PreEventWebhookEndpoint -IncludedEventType $IncludedEventTypes -Destination $dest
# Azure Function
$dest = New-AzEventGridAzureFunctionEventSubscriptionDestinationObject -ResourceId "<Azure Function Resource Id>"
New-AzEventGridSystemTopicEventSubscription -ResourceGroupName $ResourceGroupForSystemTopic -SystemTopicName $SystemTopicName -EventSubscriptionName $EventSubscriptionName -Destination $dest -IncludedEventType $IncludedEventTypes
Create a maintenance configuration by following the steps listed here.
SystemTopicName="<System topic name>
# Use the same Resource Group that you used to create maintenance configuration in Step 1
ResourceGroupName="<Resource Group mentioned in Step 1>"
# Obtain the Maintenance Configuration ID from Step 1 and assign it to Source variable
Source="/subscriptions/<subId>/resourceGroups/<Resource group>/providers/Microsoft.Maintenance/maintenanceConfigurations/<Maintenance configuration Name>"
TopicType="Microsoft.Maintenance.MaintenanceConfigurations"
Location="<System topic location> "
# System topic creation
az eventgrid system-topic create --name $SystemTopicName --resource-group $ResourceGroupName --source $Source --topic-type $TopicType --location $Location
# Event subscription creation
IncludedEventTypes='("Microsoft.Maintenance.PreMaintenanceEvent")'
# Webhook
az eventgrid system-topic event-subscription create --name "<Event subscription name>" --resource-group $ResourceGroupName --system-topic-name $SystemTopicName --endpoint-type webhook --endpoint "<webhook URL>" --included-event-types IncludedEventTypes
# Azure Function
az eventgrid system-topic event-subscription create –name "<Event subscription name>" --resource-group $ResourceGroupName --system-topic-name $SystemTopicName --endpoint-type azurefunction --endpoint "<Azure Function ResourceId>" --included-event-types IncludedEventTypes
Create a maintenance configuration by following the steps listed here.
PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>?api-version=2022-06-15
PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>/eventSubscriptions/<Event Subscription name>?api-version=2022-06-15
PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>/eventSubscriptions/<Event Subscription name>?api-version=2022-06-15
Sign in to the Azure portal and go to Azure Update Manager.
Under Manage, select Machines, Maintenance Configurations.
In the Maintenance Configuration page, select the maintenance configuration to which you want to add a pre and post event.
In the selected Maintenance configuration page, under Settings, select Events. Alternatively, under the Overview, select the card Create a maintenance event.
Select +Event Subscription to create Pre/Post Maintenance Event.
On the Create Event Subscription page, enter the following details:
In the Event Subscription Details section, provide an appropriate name.
Keep the schema as Event Grid Schema.
In the Topic Details section, provide an appropriate name to the System Topic Name.
In the Event Types section, Filter to Event Types, select the event types that you want to get pushed to the endpoint or destination. You can select between Pre Maintenance Event and Post Maintenance Event. To learn more about event types that are specific to schedule maintenance configurations, see Azure Event Types.
In the Endpoint details section, select the endpoint from where you want to receive the response from.
Select Create to configure the pre and post events on an existing schedule.
PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>?api-version=2022-06-15
PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>/eventSubscriptions/<Event Subscription name>?api-version=2022-06-15
PUT /subscriptions/<subscription Id>/resourceGroups/<resource group name>/providers/Microsoft.EventGrid/systemTopics/<system topic name>/eventSubscriptions/<Event Subscription name>?api-version=2022-06-15