Create and monitor Media Services events with Event Grid using the Azure CLI

Azure Event Grid is an eventing service for the cloud. This service uses event subscriptions to route event messages to subscribers. Media Services events contain all the information you need to respond to changes in your data. You can identify a Media Services event because the eventType property starts with "Microsoft.Media.". For more information, see Media Services event schemas.

In this article, you use the Azure CLI to subscribe to events for your Azure Media Services account. Then, you trigger events to view the result. Typically, you send events to an endpoint that processes the event data and takes actions. In this article, you send the events to a web app that collects and displays the messages.

Prerequisites

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

  • Install and use the CLI locally, this article requires the Azure CLI version 2.0 or later. Run az --version to find the version you have. If you need to install or upgrade, see Install the Azure CLI.

    Currently, not all Media Services v3 CLI commands work in the Azure Cloud Shell. It is recommended to use the CLI locally.

  • Create a Media Services account.

    Make sure to remember the values that you used for the resource group name and Media Services account name.

Create a message endpoint

Before subscribing to the events for the Media Services account, let's create the endpoint for the event message. Typically, the endpoint takes actions based on the event data. In this article, you deploy a pre-built 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. Select Deploy to Azure to deploy the solution to your subscription. In the Azure portal, provide values for the parameters.

  2. The deployment may take a few minutes to complete. After the deployment has succeeded, view your web app to make sure it's running. In a web browser, navigate to: https://<your-site-name>.azurewebsites.net

If you switch to the "Azure Event Grid Viewer" site, you see it has no events yet.

Enable Event Grid resource provider

If you haven't previously used Event Grid in your Azure subscription, you may need to register the Event Grid resource provider.

In the Azure portal:

  1. Select Subscriptions.
  2. Select the subscription you're using for Event Grid.
  3. Under Settings, select Resource providers.
  4. Find Microsoft.EventGrid.
  5. If not registered, select Register.

It may take a moment for the registration to finish. Select Refresh to update the status. When Status is Registered, you're ready to continue.

Set the Azure subscription

In the following command, provide the Azure subscription ID that you want to use for the Media Services account. You can see a list of subscriptions that you have access to by navigating to Subscriptions.

az account set --subscription mySubscriptionId

Subscribe to Media Services events

You subscribe to an article to tell Event Grid which events you want to track. The following example subscribes to the Media Services account you created, and passes the URL from the website you created as the endpoint for event notification.

Replace <event_subscription_name> with a unique name for your event subscription. For <resource_group_name> and <ams_account_name>, use the values you used when creating the Media Services account. For the <endpoint_URL>, provide the URL of your web app and add api/updates to the home page URL. By specifying the endpoint when subscribing, Event Grid handles the routing of events to that endpoint.

  1. Get the resource id

    amsResourceId=$(az ams account show --name <ams_account_name> --resource-group <resource_group_name> --query id --output tsv)
    

    For example:

    amsResourceId=$(az ams account show --name amsaccount --resource-group amsResourceGroup --query id --output tsv)
    
  2. Subscribe to the events

    az eventgrid event-subscription create \
    --resource-id $amsResourceId \
    --name <event_subscription_name> \
    --endpoint <endpoint_URL>
    

    For example:

    az eventgrid event-subscription create --resource-id $amsResourceId --name amsTestEventSubscription --endpoint https://amstesteventgrid.azurewebsites.net/api/updates/
    

    Tip

    You might get validation handshake warning. Give it a few minutes and the handshake should validate.

Now, let's trigger events to see how Event Grid distributes the message to your endpoint.

Send an event to your endpoint

You can trigger events for the Media Services account by running an encoding job. You can follow this quickstart to encode a file and start sending events.

View your web app again, and notice that a subscription validation event has been sent to it. Event Grid sends the validation event so the endpoint can verify that it wants to receive event data. The endpoint has to set validationResponse to validationCode. For more information, see Event Grid security and authentication. You can view the web app code to see how it validates the subscription.

Tip

Select the eye icon to expand the event data. Do not refresh the page, if you want to view all the events.

View subscription event

Next steps

Upload, encode, and stream