Page 26 - MSDN Magazine, September 2018
P. 26
Azure Storage
Event Grid Storage Account Topic
Logic App
Notice that both the max-delivery-attempts and event-ttl settings are included. It’s not a requirement to include both settings, but they can be used together to configure the retry policies. I’m setting the maximum number of delivery attempts to two and the time to live to one minute. Another new argument called dead- letter-endpoint is initialized to the storage account
container using the variable that was created earlier. It’s time to send some events and see this working end-to-end.
Sending Events
From the CLI, I can copy a sample body that contains a song request. This request will actually contain an invalid music genre (Rock) that will be rejected by the event handler.
# copy the request body
body=$(eval echo "'$(curl https://raw.githubusercontent.com/dbarkol/ azure-event-grid-patterns/master/badsongrequest.json)'")
# post the request to the custom topic endpoint
curl -X POST -H "aeg-sas-key: $topickey" -d "$body" $topicendpoint
The expectation is that a few minutes after the message is sent, it will end up in the storage account container that I configured as the
Office 365 Email
Figure 5 Blob Events Handled by Logic Apps
I’ll be sending the events to a custom Event Grid topic. Let’s create the topic and save the endpoint address and access key. These values will be leveraged later when publishing events:
# create custom topic
az eventgrid topic create -g $rgname –-name $topicname -l westus2
# save topic endpoint
topicendpoint=$(az eventgrid topic show –-name $topicname -g $rgname
–-query “endpoint” –-output tsv)
# save topic key
topickey=$(az eventgrid topic key list --name $topicname -g $rgname
--query "key1" --output tsv)
Azure Functions Event Handler
In this scenario, I’ll pretend that users are sending song requests to a fic- titious radio station that specializes in Blues music.
As requests come in, they’re put onto a playlist for
the station. However, if the genre of the song doesn’t
match the target music for the radio station, it will be rejected and placed on the dead letter channel. The event handler is an Azure Function built on the version 2 runtime. It will inspect the Subject field of the event and approve or reject the song request accordingly. The code for the
Azure Function is shown in Figure 4.
This function has two parts. The first checks to see if the event coming in is intended for val- idating the endpoint. If it’s a validation request, the function returns the validation code to prove ownership and acceptance of incoming messages. The second part of the function is for event notifications from Event Grid. By returning a bad request response (401), an explicit statement is made to no longer send the event to the han- dler. This brings me to the most important part of the process—creating an event subscription.
Subscribing to Events
With all the pieces in place, it’s finally time to create an event subscription that demonstrates both the retry policies and dead lettering fea- ture. An assumption is that the Azure Function in Figure 4 has been deployed and is currently running in Azure. The command to create the subscription is as follows:
az eventgrid event-subscription create \ --endpoint <your-azure-function-url> \ --topic-name $topicname \
-g $rgname \
--name song-request-sub \ --deadletter-endpoint $containerid \ --max-delivery-attempts 2 --event-ttl 1
Figure 6 An Event Grid-Triggered Logic App
18 msdn magazine
Azure