title | description | author | ms.topic | ms.service | ms.author | ms.date | ms.custom |
---|---|---|---|---|---|---|---|
Send events from Azure API Management to Event Grid |
In this quickstart, you enable Event Grid events for your Azure API Management instance, then send events to a sample application. |
dlepow |
how-to |
api-management |
danlep |
11/2/2021 |
ignite-fall-2021 |
API Management integrates with Azure Event Grid so that you can send event notifications to other services and trigger downstream processes. Event Grid is a fully managed event routing service that uses a publish-subscribe model. Event Grid has built-in support for Azure services like Azure Functions and Azure Logic Apps, and can deliver event alerts to non-Azure services using webhooks.
For example, using integration with Event Grid, you can build an application that updates a database, creates a billing account, and sends an email notification each time a user is added to your API Management instance.
In this article, you subscribe to Event Grid events in your API Management instance, trigger events, and send the events to an endpoint that processes the data. To keep it simple, you send events to a sample web app that collects and displays the messages:
:::image type="content" source="media/how-to-event-grid/event-grid-viewer-intro.png" alt-text="API Management events in Event Grid viewer":::
[!INCLUDE azure-cli-prepare-your-environment.md]
- If you don't already have an API Management service, complete the following quickstart: Create an Azure API Management instance
- Enable a system-assigned managed identity in your API Management instance.
- Create a resource group if you don't have one in which to deploy the sample endpoint.
In this section, you use a Resource Manager template to deploy a pre-built sample web application to Azure App Service. Later, you subscribe to your API Management instance's Event Grid events and specify this app as the endpoint to which the events are sent.
To deploy the sample app, you can use the Azure CLI, Azure PowerShell, or the Azure portal. The following example uses the az deployment group create command in the Azure CLI.
-
Set
RESOURCE_GROUP_NAME
to the name of an existing resource group -
Set
SITE_NAME
to a unique name for your web appThe site name must be unique within Azure because it forms part of the fully qualified domain name (FQDN) of the web app. In a later section, you navigate to the app's FQDN in a web browser to view the events.
RESOURCE_GROUP_NAME=<your-resource-group-name>
SITE_NAME=<your-site-name>
az deployment group create \
--resource-group $RESOURCE_GROUP_NAME \
--template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \
--parameters siteName=$SITE_NAME hostingPlanName=$SITE_NAME-plan
Once the deployment has succeeded (it might take a few minutes), open a browser and navigate to your web app to make sure it's running:
https://<your-site-name>.azurewebsites.net
You should see the sample app rendered with no event messages displayed.
[!INCLUDE event-grid-register-provider-portal.md]
In Event Grid, you subscribe to a topic to tell it which events you want to track, and where to send them. Here, you create a subscription to events in your API Management instance.
-
In the Azure portal, navigate to your API Management instance.
-
Select Events > + Event Subscription.
-
On the Basic tab:
- Enter a descriptive Name for the event subscription.
- In Event Types, select one or more API Management event types to send to Event Grid. For the example in this article, select at least Microsoft.APIManagement.ProductCreated
- In Endpoint Details, select the Web Hook event type, click Select an endpoint, and enter your web app URL followed by
api/updates
. Example:https://myapp.azurewebsites.net/api/updates
. - Select Confirm selection.
-
Leave the settings on the remaining tabs at their default values, and then select Create.
:::image type="content" source="media/how-to-event-grid/create-event-subscription.png" alt-text="Create an event subscription in Azure portal":::
Now that the sample app is up and running and you've subscribed to your API Management instance with Event Grid, you're ready to generate events.
As an example, create a product in your API Management instance. If your event subscription includes the Microsoft.APIManagement.ProductCreated event, creating the product triggers an event that is pushed to your web app endpoint.
Navigate to your Event Grid Viewer web app, and you should see the ProductCreated
event. Select the button next to the event to show the details.
:::image type="content" source="media/how-to-event-grid/event-grid-viewer-product-created.png" alt-text="Product created event in Event Grid viewer":::
API Management event data includes the resourceUri
, which identifies the API Management resource that triggered the event. For details about the API Management event message schema, see the Event Grid documentation: