In this example, we are going to route events from Event Grid to an Azure function. First, we need to create a new Event Grid Topic in Azure. To create this, follow these steps:
Azure Event Grid is also covered in Chapter 9, Configuring Serverless Computing.
- Navigate to the Azure portal by opening https://portal.azure.com.
- Click Create a resource, type Event Grid Topic in the search bar, and create a new Event Grid Topic.
- Add the following values:
- Name: PacktEventGridTopic.
- Subscription: Pick a subscription.
- Resource group: Create a new one and call it PacktEventGridResourceGroup.
- Location: East US.
- Event Schema: Event Grid Schema.
- Click Create.
- Next, we are going to create a new Azure function.
- Click Create a resource, type Function App in the search bar, and create a new function app.
- Add the following values:
- Subscription: Pick a subscription.
- Resource group: Select PacktEventGridResourceGroup.
- Function App Name: PacktEventGridFunction.
- Publish: Code.
- Runtime Stack: .NET Core.
- Region: East US.
- Click Review + create and then create.
- When the function app is created, navigate to the settings and click on the function app:
Selecting the Azure function app
- Click the +, on the right-hand side of Functions. Then, select in-portal to create the function app in the Azure portal:
Creating a function in the portal
- Click Continue.
- Then, click More templates... and then Finish and view templates. In the next screen of the wizard, scroll down a bit and select Azure Event Grid trigger:
Selecting the Azure Event Grid trigger
- If you get a notification that Microsoft.Azure.WebJobs.Extensions.EventGrid is not installed, then install it.
- Keep the default settings, and then click Create:
Creating a new trigger
- When the trigger is created, the run.csx file is opened by default. Click on the Add Event Grid subscription link in the top menu:
Selecting Event Grid subscription
- Add the following values:
- Name: PacktEventSubscription.
- Event Schema: Event Grid Schema.
- Topic Types: Event Grid Topics.
- Subscription: Pick a subscription.
- Resource group: Select PackEventGridResourceGroup.
-
- Resource: PacktEventGridTopic:
Creating the event subscription
- Click Create. This will create a new event subscription that subscribes to the
Event Grid Topic. - Now, open PowerShell, and run the following commands.
- First, we need to log in to the Azure account:
Connect-AzAccount
- If necessary, select the correct subscription:
Select-AzSubscription -SubscriptionId "********-****-****-****-***********"
- Get a reference to the endpoint and the key:
$endpoint = (Get-AzEventGridTopic `
-ResourceGroupName PacktEventGridResourceGroup `
-Name PacktEventGridTopic).Endpoint
$keys = Get-AzEventGridTopicKey `
-ResourceGroupName PacktEventGridResourceGroup `
-Name PacktEventGridTopic
- Create a random event ID and sort the date and time:
$eventID = Get-Random 99999
$eventDate = Get-Date -Format s
- Construct the body using a hash table:
$htbody = @{
id= $eventID
eventType="recordInserted"
subject="myapp/packtpub/books"
eventTime= $eventDate
data= @{
title="Microsoft Azure Architect Technologies"
eventtype="Ebook"
}
dataVersion="1.0"
- Use ConvertTo-Json to convert the event body from the hash table to a JSON object. Append square brackets to the converted JSON payload, since they are expected in the event's JSON payload syntax:
$body = "["+(ConvertTo-Json $htbody)+"]"
Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers
@{"aeg-sas-key" = $keys.Key1}
- This will create a custom event. You can now check the Azure Function logs for the result (the Azure Function logs have a delay of 5 minutes).
In this demonstration, we've routed an event from Azure Event Grid to an Azure function. In the next section, we are going to cover how to design an effective messaging architecture.