Azure Functions -Process events with a serverless code architecture

What’s up guys, Eze is here. During the past post we were talking about Firebase, AngularJs and a bunch of cool tools to create rapidly powerful front-end apps, in case you missed it you can check here. Today I want to bring you a server side code that runs in a serverless architecture, yes we are going to talk about Azure Functions.

About Azure Functions

This is a cool feature that Azure team has created for us. The idea is to mix events + code to execute in a serverless environment which basically means you don’t need to take care about where your code is going to run, about which configuration has this server, how much memory is using or which OS version has. You just need to define the code to execute and which event is going to trigger our function. As they say.

An event-based serverless compute experience to accelerate your development. It can scale based on demand and you pay only for the resources you consume.

Common scenarios for Azure Functions

Trigger by time: Azure functions uses CRON job syntax to expecify an event based on a timer. So lets say that we want our code to run every 10 minutes.

Azure service event: We can setup our function to execute every time something happen in an Azure service. For example when a new file is uploaded in a blob storage.

Serverless web application architectures*: This one in particular is the one I’ve used for our example. In this case a front-end app calls a function to execute a procedure in a server side.

Function components

As you can see in the image below the Azure function is made by two files the first one is a function.json that represents the structure of our function, basically the metadata of our function. The second one is a run.csx file (because I’m using C#) which is directly the method we want our function to execute. Microsoft offers us a web editor to edit directly in the browser our functions but if we want we can configure a github project and setup an automatic deploy when we push our repository.

function.png

I have a working example in this link if you want to see the complete implementation. In this example every time a user makes a request in our front-end app we call this Azure Function to store the request in an Azure storage table as you ca see below

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ICollector<Request> outTable, TraceWriter log)
{
dynamic data = await req.Content.ReadAsAsync<object>();
string name = data?.userName;

if (name == null)
{
return req.CreateResponse(HttpStatusCode.BadRequest, "Please provide an user name in the request body");
}

outTable.Add(new Request()
{
PartitionKey = "Functions",
RowKey = Guid.NewGuid().ToString(),
cityName = data.cityName,
convertedLocalTime = data.convertedLocalTime,
convertedRequestedTime = data.convertedRequestedTime,
dstOffset = data.dstOffset,
rawOffset = data.rawOffset,
timeAtLocation = data.timeAtLocation,
timeZoneId = data.timeZoneId,
timeZoneName = data.timeZoneName,
userName = data.userName
});
return req.CreateResponse(HttpStatusCode.Created);
}

For more details of Azure Functions you can use this link.

If you found this post useful please don’t forget to press the like button and share it. If you are in doubt don’t hesitate to ask a question and as always thank you for reading.

Advertisements

3 thoughts on “Azure Functions -Process events with a serverless code architecture

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