Nowadays and increasingly more, we often hear the term serverless, but we aren’t quite sure what it is and what it means.

What is serverless?

As the name indicates, it’s the setting up of “serverless” apps. But when we say serverless, it’s not that we pyhsically don’t have anything there, but, relying on cloud services, we detach from all that goes into building a server-based infrastructure. We only focus on what interests us; the development of our app. The idea is that we don’t need to install virtual machines, but we rely on the use of function-oriented services (for example, Azure Functions). In this way, we break down the funcionality into small units, a similar concept to microservices.One of the main advantages of using this type of infrastructures, apart from “forgetting” about the server side of things, is that its charges for seconds of use and not for minutes or hours like other services, which results in significant savings.In short, serverless is the abstraction of servers, infrastructures, and operating systems. If you want to know a bit more about it you can read our blog post “Going serverless”.

What is Azure Event Grid?

Azure Event Grid is an event routing service from Azure, which also allows us to build “serverless” apps.

Before continuing, here are the definitions of some important concepts to know:
– Event Publisher: origen that generates an event.
– Event: small set of information that you wish to transmit. Each event has the following information: who or what generated the event, at what moment did it happen, what type is it and an identifier.
– Topic: is the way and concept by which Azure Event Grid works, to receieve the generated events by publishers. It’s also where we can subscribe to be informed when these events are generated.
– Subscription: the link of applications or servers with one or varios topics inside Azure Event Grid.
– Event manager: the services or applications that these events will address, once they have arrived to Azure Event Grid.

Small demo

Now we’ve been introduced to this serverless concept and Azure Event Grid itself, let’s see a small demonstration.
The first thing that we have to do is enter in the Azure portal with a valid subscribtion, and create Azure Event Grid topic.


Next you enter a subscription, this is subscribed to any event that event grid can receive and route. For this you can use a logic app.

 

We use the connecter to subscribe to the previously created topic.

To verify that the events are received, we are going to configure another step in the logic App. In this case we will put a step to send emails:


Finally, using Powershell, we can send an event to Event Grid:

 $endpoint = (Get-AzureRmEventGridTopic -ResourceGroupName gridResourceGroup -Name BrintiaTopicPOC).Endpoint

$keys = Get-AzureRmEventGridTopicKey -ResourceGroupName gridResourceGroup -Name BrintiaTopicPOC

 

$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/poc”

    eventTime= $eventDate  

    data= @{

        make=”test”

    }

    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)+”]”

 

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

As a final result, we see how we receive an email to the account configured in the logic step:

Written by: Pedro Sanchez