Get startedGet started for free

Connecting Event Grid Topic

1. Connecting Event Grid Topic

In this video, we will go through the process of creating an Event Grid topic. We will also see how we can send events to an Event Grid Topic. First, we will search for Event Grid in the search bar. Once we are in the Event Grid configuration area, there are several types of services we can choose from. There are namespaces we can use for receiving Internet of Things device telemetry. There are system topics for handling events from Azure's own infrastructure and services, but what we are interested in is a general-purpose Topic. Let's create an instance of it. The creation process is very similar to creating any other resource type in Azure. We select the resource group. We give our resource a valid name. We select the region. Once we are happy, we go to "Review + create". Then, after reviewing the details, we create the resource. While our service is being deployed, we can have a look at an existing instance of an Event Grid Topic. To do so, we will navigate to Event Grid, scroll down to Topics, and select the View option. Then we will choose the instance called "sales-event-processor". Then, we will open a different tab with Azure Portal. We have a function app that is configured to send events to this Event Grid Topic. We'll select it. To connect a function app, or any other app, to an Event Grid Topic, we will need to obtain the Topic endpoint URL and the secret key. In Azure Functions, these are configured under Settings, in the Environment variables section. These are the variables that will be accessible by any functions in the same Function App. Here, we have variables called EVENT_GRID_TOPIC_ENDPOINT and EVENT_GRID_TOPIC_KEY. EVENT_GRID_TOPIC_ENDPOINT is the URL that can be found on the Overview page of the Event Grid Topic instance. It can be copied from the Overview page and pasted into the Value of the environment variable. Access Key is found under the Settings section of the Event Grid topic, in the Access keys option. The page has two access keys, both of which can be viewed and copied. We will need either of these. We just need to paste the value into the value field of the EVENT_GRID_TOPIC_KEY environment variable of the Function App. Once we configured the connection, we can go back to the Overview page of the Function App and select the HTTP trigger function. This function contains JavaScript code that makes a HTTP POST request to the Event Grid Topic. To do so, we extract the endpoint address from the EVENT_GRID_TOPIC_ENDPOINT environment variable. Then, to secure our request, we extract the key from the EVENT_GRID_TOPIC_KEY environment variable. Then, we construct the event payload, which will be the body of the POST request. It's been structured to adhere to a specific Event Grid schema. Else, Event Grid Topic will not accept it. Then we are sending the actual POSt request and returning different response back to the caller of this function, depending on whether or not Event Grid accepted our request and the event has been published. The request goes to the endpoint we defined before. And, to secure it, we pass the access key to the "aeg-sas-key" request header. We can test our function to see if it will publish the event successfully once invoked. And it does based on 200 being returned as the HTTP code. This concludes the overview of setting up Event Grid Topic connection.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.