Azure Functions : A quick demo when using Event Hub

Introduction

Azure functions is the “serverless” offering on Azure. Serverless doesn’t mean there aren’t any servers, but it’s rather a platform service where you can run your own snippets of code. Each code can be triggered, where it then can have an in and/or output. In terms of billing, you pay for the amount of runtime you consume.

Imagine having a stateless web front end which publishes state onto a queue or storage account. Then Azure functions is being triggered and your business logic starts. Sounds familiar? Yes, because that’s a typical flow you normally do in your application stacks. Now you can segment that even more into services you do not need to manage.

 

Demo

Today I’m going to show you a very brief demo on using an Azure Event Hub as trigger/input for an Azure function. So what’s the flow we’ll be doing?

  • A client will put a message on the event hub queue
  • This will trigger an Azure function
  • The function will save the contents of the message onto a storage account

Sound simple right? And it actually is that simple to do too!

 

 

Event Hub

We’ll start off with creating an out-of-the-box eventhub ;

2016-12-02-19_59_28-shared-access-policies-microsoft-azure
Where we create a connection string to access it ; 

2016-12-02-20_02_32-shared-access-policies-microsoft-azure

Nothing fancy here!

 

Function

Now let’s create a function.

2016-12-02-20_01_08-program-manager

We’re using “event hub” as a trigger, and then this will also act as an “input”. I’ll be using a storage account as an output, as I want to save the messages there. The connection towards the eventhub might have been the most “tricky” one, but that was far from complex… Just create a new connection and add the connection string ;

2016-12-02-20_01_26-add-service-bus-connection-microsoft-azure

 

Now let’s take a look at the code of this function…

2016-12-02-20_00_28-function-app-microsoft-azure

 module.exports = function (context, myEventHubTrigger) {
context.log('JavaScript eventhub trigger function processed work item', myEventHubTrigger);
context.bindings.outputBlob = myEventHubTrigger;
context.done();
}; 

I added the second line of code, and that was it. We can easily say this isn’t exactly rocket science… For me, this flow was the first time I used nodejs, and I was able to get this working.

 

Demo

As a client I started from the following code ; https://github.com/ytechie/eventhubs-js/

Here I updated the /config/config.json file ;

{
"EventHubsNamespace": "tdiottest",
"EventHubsHubName": "functionstest",
"DeviceName": "testclient",
"EventHubsKeyName": "fullaccess",
"EventHubsKey": "mysecretkey="
}

And I tweaked the example file to send two messages to the event hub ; nodejs sendmsg.js

These messages get sent to the eventhub, as seen in the monitoring…

2016-12-02-20_14_02-tdiottest-microsoft-azure

We can see the events being processed in the logs ;

2016-12-02-20_18_53-function-app-microsoft-azure

And suddenly they are in the storage account under the path I configured ;

2016-12-02-20_14_25-program-manager

 

Closing Notes

What did I learn by doing this experiment?

  • Setting up functions is very easy
  • When doing a load test, I posted about a million messages onto my queue. Then I saw that I was getting connection drops when trying to push towards the storage account. So do not forget error handling! Which I kinda did not foresee…
  • The eventhub trigger is also an input. I didn’t grasp that at first…

Anyhow, I hope this was insightful. Have fun experimenting!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s