Routing events using Event Grid

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 9Configuring Serverless Computing
  1. Navigate to the Azure portal by opening https://portal.azure.com.
  2. Click Create a resource, type Event Grid Topic in the search bar, and create a new Event Grid Topic.
  3. Add the following values:
    1. Name: PacktEventGridTopic.
    2. Subscription: Pick a subscription.
    3. Resource group: Create a new one and call it PacktEventGridResourceGroup.
    4. Location: East US.
    5. Event Schema: Event Grid Schema.
  4. Click Create.
  5. Next, we are going to create a new Azure function. 
  6. Click Create a resource, type Function App in the search bar, and create a new function app.
  7. Add the following values:
    1. Subscription: Pick a subscription.
    2. Resource group: Select PacktEventGridResourceGroup.
    3. Function App Name: PacktEventGridFunction.
    4. Publish: Code.
    5. Runtime Stack: .NET Core.
    6. Region: East US.
  8. Click Review + create and then create.
  9. When the function app is created, navigate to the settings and click on the function app:

Selecting the Azure function app
  1. 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
  1. Click Continue.
  1. 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
  1. If you get a notification that Microsoft.Azure.WebJobs.Extensions.EventGrid is not installed, then install it.
  2. Keep the default settings, and then click Create:

Creating a new trigger
  1. 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
  1. Add the following values:
    1. Name: PacktEventSubscription.
    2. Event Schema: Event Grid Schema.
    3. Topic Types: Event Grid Topics.
    4. Subscription: Pick a subscription.
    5. Resource group: Select PackEventGridResourceGroup.
    1. Resource: PacktEventGridTopic:

Creating the event subscription
  1. Click Create. This will create a new event subscription that subscribes to the
    Event Grid Topic.
  2. Now, open PowerShell, and run the following commands. 
  3. First, we need to log in to the Azure account:
Connect-AzAccount
  1. If necessary, select the correct subscription:
Select-AzSubscription -SubscriptionId "********-****-****-****-***********"
  1. Get a reference to the endpoint and the key:
$endpoint = (Get-AzEventGridTopic `
-ResourceGroupName PacktEventGridResourceGroup `
-Name PacktEventGridTopic).Endpoint

$keys = Get-AzEventGridTopicKey `
-ResourceGroupName PacktEventGridResourceGroup `
-Name PacktEventGridTopic
  1. Create a random event ID and sort the date and time:
$eventID = Get-Random 99999
$eventDate = Get-Date -Format s
  1. 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"
  1. 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}
  1. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.204.201