Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (2024)

  • Article

Event Grid is a fully managed service that enables you to easily manage events across many different Azure services and applications. It simplifies building event-driven and serverless applications. For an overview of the service, see Event Grid overview.

In this article, you use the Azure portal to do the following tasks:

  1. Create a custom topic.
  2. Subscribe to the custom topic.
  3. Trigger the event.
  4. View the result. Typically, you send events to an endpoint that processes the event data and takes actions. However, to simplify this article, you send the events to a web app that collects and displays the messages.

Prerequisites

If you don't have an Azure subscription, create an Azure free account before you begin.

Register the Event Grid resource provider

Unless you've used Event Grid before, you'll need to register the Event Grid resource provider. If you’ve used Event Grid before, skip to the next section.

In the Azure portal, do the following steps:

  1. On the left menu, select Subscriptions.

  2. Select the subscription you want to use for Event Grid from the subscription list.

  3. On the Subscription page, select Resource providers under Settings on the left menu.

  4. Search for Microsoft.EventGrid, and confirm that the Status is Not Registered.

  5. Select Microsoft.EventGrid in the provider list.

  6. Select Register on the command bar.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (1)

  7. Refresh to make sure the status of Microsoft.EventGrid is changed to Registered.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (2)

Create a custom topic

An Event Grid topic provides a user-defined endpoint that you post your events to.

  1. Sign in to Azure portal.

  2. In the search bar at the topic, type Event Grid Topics, and then select Event Grid Topics from the drop-down list.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (3)

  3. On the Event Grid Topics page, select + Create on the toolbar.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (4)

  4. On the Create Topic page, follow these steps:

    1. Select your Azure subscription.

    2. Select an existing resource group or select Create new, and enter a name for the resource group.

    3. Provide a unique name for the custom topic. The topic name must be unique because it's represented by a DNS entry. Don't use the name shown in the image. Instead, create your own name - it must be between 3-50 characters and contain only values a-z, A-Z, 0-9, and "-".

    4. Select a location for the Event Grid topic.

    5. Select Review + create at the bottom of the page.

      Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (5)

    6. On the Review + create tab of the Create topic page, select Create.

      Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (6)

  5. After the deployment succeeds, select Go to resource to navigate to the Event Grid Topic page for your topic. Keep this page open. You use it later in the quickstart.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (7)

    Note

    To keep the quickstart simple, you'll be using only the Basics page to create a topic. For detailed steps about configuring network, security, and data residency settings on other pages of the wizard, see Create a custom topic.

Create a message endpoint

Before you create a subscription for the custom topic, create an endpoint for the event message. Typically, the endpoint takes actions based on the event data. To simplify this quickstart, you deploy a prebuilt web app that displays the event messages. The deployed solution includes an App Service plan, an App Service web app, and source code from GitHub.

  1. In the article page, select Deploy to Azure to deploy the solution to your subscription. In the Azure portal, provide values for the parameters.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (8)

  2. On the Custom deployment page, do the following steps:

    1. For Resource group, select the resource group that you created when creating the storage account. It will be easier for you to clean up after you're done with the tutorial by deleting the resource group.

    2. For Site Name, enter a name for the web app.

    3. For Hosting plan name, enter a name for the App Service plan to use for hosting the web app.

    4. Select Review + create.

      Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (9)

  3. On the Review + create page, select Create.

  4. The deployment may take a few minutes to complete. Select Alerts (bell icon) in the portal, and then select Go to resource group.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (10)

  5. On the Resource group page, in the list of resources, select the web app that you created. You also see the App Service plan and the storage account in this list.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (11)

  6. On the App Service page for your web app, select the URL to navigate to the web site. The URL should be in this format: https://<your-site-name>.azurewebsites.net.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (12)

  7. Confirm that you see the site but no events have been posted to it yet.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (13)

Subscribe to custom topic

You subscribe to an Event Grid topic to tell Event Grid which events you want to track, and where to send the events.

  1. Now, on the Event Grid Topic page for your custom topic, select + Event Subscription on the toolbar.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (14)

  2. On the Create Event Subscription page, follow these steps:

    1. Enter a name for the event subscription.

    2. Select Web Hook for the Endpoint type.

    3. Choose Select an endpoint.

      Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (15)

    4. For the web hook endpoint, provide the URL of your web app and add api/updates to the home page URL. Select Confirm Selection.

      Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (16)

    5. Back on the Create Event Subscription page, select Create.

  3. View your web app again, and notice that a subscription validation event has been sent to it. Select the eye icon to expand the event data. Event Grid sends the validation event so the endpoint can verify that it wants to receive event data. The web app includes code to validate the subscription.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (17)

Send an event to your topic

Now, let's trigger an event to see how Event Grid distributes the message to your endpoint. Use either Azure CLI or PowerShell to send a test event to your custom topic. Typically, an application or Azure service would send the event data.

The first example uses Azure CLI. It gets the URL and key for the custom topic, and sample event data. Use your custom topic name for <topic name>. It creates sample event data. The data element of the JSON is the payload of your event. Any well-formed JSON can go in this field. You can also use the subject field for advanced routing and filtering. CURL is a utility that sends HTTP requests.

Azure CLI

  1. In the Azure portal, select Cloud Shell. The Cloud Shell opens in the bottom pane of the web browser.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (18)

  2. Select Bash in the top-left corner of the Cloud Shell window.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (19)

  3. Run the following command to get the endpoint for the topic: After you copy and paste the command, update the topic name and resource group name before you run the command. You publish sample events to this topic endpoint.

    endpoint=$(az eventgrid topic show --name <topic name> -g <resource group name> --query "endpoint" --output tsv)
  4. Run the following command to get the key for the custom topic: After you copy and paste the command, update the topic name and resource group name before you run the command. It's the primary key of the Event Grid topic. To get this key from the Azure portal, switch to the Access keys tab of the Event Grid Topic page. To be able post an event to a custom topic, you need the access key.

    key=$(az eventgrid topic key list --name <topic name> -g <resource group name> --query "key1" --output tsv)
  5. Copy the following statement with the event definition, and press ENTER.

    event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/motorcycles", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "make": "Ducati", "model": "Monster"},"dataVersion": "1.0"} ]'
  6. Run the following Curl command to post the event: In the command, aeg-sas-key header is set to the access key you got earlier.

    curl -X POST -H "aeg-sas-key: $key" -d "$event" $endpoint

Azure PowerShell

The second example uses PowerShell to do similar steps.

  1. In the Azure portal, select Cloud Shell (alternatively go to https://shell.azure.com/). The Cloud Shell opens in the bottom pane of the web browser.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (20)

  2. In the Cloud Shell, select PowerShell in the top-left corner of the Cloud Shell window. See the sample Cloud Shell window image in the Azure CLI section.

  3. Set the following variables. After you copy and paste each command, update the topic name and resource group name before you run the command:

    Resource group:

    $resourceGroupName = "<resource group name>"

    Event Grid topic name:

    $topicName = "<topic name>"
  4. Run the following commands to get the endpoint and the keys for the topic:

    $endpoint = (Get-AzEventGridTopic -ResourceGroupName $resourceGroupName -Name $topicName).Endpoint$keys = Get-AzEventGridTopicKey -ResourceGroupName $resourceGroupName -Name $topicName
  5. Prepare the event. Copy and run the statements in the Cloud Shell window.

    $eventID = Get-Random 99999#Date format should be SortableDateTimePattern (ISO 8601)$eventDate = Get-Date -Format s#Construct body using Hashtable$htbody = @{ id= $eventID eventType="recordInserted" subject="myapp/vehicles/motorcycles" eventTime= $eventDate data= @{ make="Ducati" model="Monster" } dataVersion="1.0"}#Use ConvertTo-Json to convert event body from Hashtable to 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)+"]"
  6. Use the Invoke-WebRequest cmdlet to send the event.

    Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers @{"aeg-sas-key" = $keys.Key1}

Verify in the Event Grid Viewer

You've triggered the event, and Event Grid sent the message to the endpoint you configured when subscribing. View your web app to see the event you just sent.

Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (21)

Clean up resources

If you plan to continue working with this event, don't clean up the resources created in this article. Otherwise, delete the resources you created in this article.

  1. Select Resource Groups on the left menu. If you don't see it on the left menu, select All Services on the left menu, and select Resource Groups.

    Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (22)

  2. Select the resource group to launch the Resource Group page.

  3. Select Delete resource group on the toolbar.

  4. Confirm deletion by entering the name of the resource group, and select Delete.

    The other resource group you see in the image was created and used by the Cloud Shell window. Delete it if you don't plan to use the Cloud Shell window later.

Next steps

Now that you know how to create custom topics and event subscriptions, learn more about what Event Grid can help you do:

  • About Event Grid
  • Route Blob storage events to a custom web endpoint
  • Monitor virtual machine changes with Azure Event Grid and Logic Apps
  • Stream big data into a data warehouse

See the following samples to learn about publishing events to and consuming events from Event Grid using different programming languages.

  • Azure Event Grid samples for .NET
  • Azure Event Grid samples for Java
  • Azure Event Grid samples for Python
  • Azure Event Grid samples for JavaScript
  • Azure Event Grid samples for TypeScript
Send custom events to web endpoint - Event Grid, Azure portal - Azure Event Grid (2024)
Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 5871

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.