My challenge was to start a runbook as soon as a new file was stored in Azure Blob Storage. Unfortunately there is no direct configuration option in the storage account for such notifications. One way would be to poll the storage account regularly (e.g. with a Logic App or Azure Function) to check for new files. But it’s easier with the Event Grid. Different Azure Events Topics can be created, for example for storage accounts, but also for subscriptions or container registries. Several event subscriptions can then be created for a topic, which can filter specific properties and address a wide variety of endpoints. Endpoints are runbooks, Azure Functions, Event Hub, Service Bus etc.

Create Endpoint (Runbook with Webhook)

First we need the destination of our call when a new file has been added. I use a runbook webhook. This is an HTTP post call that contains the details of the event in the request body.

The runbook should receive the parameter [object] $ WebhookData. This contains the details of the event. For example, the URL of the uploaded file could be determined from the parameter (lines 10 and 11):

param (
    [Parameter (Mandatory = $false)]
    [object] $WebhookData
)

if ($WebhookData) {
    $WebhookData.WebhookName
    $WebhookData.RequestHeader
    $WebhookData.RequestBody
    $body = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
    $body.data.url
}

When generating the webhook, the WebhookData parameter is left blank and the generated link is required for the event subscription.

Create Event Subscription

Event subscriptions can be created via the portal or via CLI. If the data source is an Azure service, it is referred to as an event system topic. An event topic and an event subscription are always created together. In the following, an event system topic is created for the storage account strgacc20201113 and the event type Blob Created. This means that the endpoint is called every time a new file is added to the storage account. The screenshot shows the selected data source (the topic) at number 1 and the event type (at number 2):

The URL for the runbook webhook can then be entered in the last section.

The condition of the event can be further restricted in the filter section. For example, I want the event to be triggered only when a new .txt file has been created in the folder1. For this purpose, the corresponding specification can be defined under Subject Filters. Under Advanced Filters, more detailed criteria can be set that refer to the body message of the event.

To create the topic and subscription with the CLI instead, only 2 commands are required. The first command creates the topic itself, that represents the storage account. The second command creates the event-subscription that defines the endpoint and the calling conditions (filters + event type):

az eventgrid system-topic create -g EventGrid --name NewFileAtStrg --location westeurope --topic-type microsoft.storage.storageaccounts --source /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/EventGrid/providers/Microsoft.Storage/storageAccounts/strgacc20201113
az eventgrid system-topic event-subscription create --name NewFileCallRunbook -g EventGrid --system-topic-name NewFileAtStrg --endpoint-type webhook --endpoint https://e53cc45c-99ab-400f-91b5-cb1b777bdb5e.webhook.we.azure-automation.net/webhooks?token=N4hV1CAgn0809iU5Ld7qOtqpiWgbZ4YW0LwOJk011gs%3d --subject-begins-with /blobServices/default/containers/cont1/blobs/folder1/ --subject-ends-with .txt --subject-case-sensitive false --included-event-types Microsoft.Storage.BlobCreated

The result is the successful call of the runbook:

The request header of the webhook post can be seen at number 1 and the body at number 2. The 3rd mark shows the extracted URL of the generated file.