Over a million developers have joined DZone.

Azure Functions: Process Events With a Serverless Code Architecture

DZone 's Guide to

Azure Functions: Process Events With a Serverless Code Architecture

This example project will give you a quick overview of Azure Functions' serverless capabilities, so you can run your code without caring where it's running from.

· Cloud Zone ·
Free Resource

What’s up guys, Eze is here. During our last posts 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 some 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 the Azure team has created for us. The idea is to mix events and code to execute in a serverless environment, which basically means you don't need to care about where your code is going to run, server configuration, how much memory you're using, or which OS version you're dealing with. 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 specify an event based on a timer. So let's say that we want our code to run every 10 minutes.

Azure service event: We can set up our function to execute every time something happens in an Azure service. For example, when a new file is uploaded into 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 on the server side.

Function Components

As you can see in the image below, the Azure function is made up by two files: The first one is function.json, which 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 the method we want our function to execute. Microsoft offers us a web editor to edit our functions directly in the browser, but if we want, we can configure a GitHub project and set up an automatic deploy when we push to our repository.


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 can 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);

And there you have it! For more details on 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.

functions as a service ,cloud ,microsoft azure functions ,serverless architecture ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}