Azure - Cloud Computing

On Azure Functions and Serverless

There’s no shortage of tools and technologies to make developers’ lives easier In the world of cloud computing, and one such offering comes from Microsoft which is Azure Functions. Here, we’ll unravel the magic behind Azure Functions and explore why it’s becoming a favourite for certain architectures.

At its core, Azure Functions is a serverless compute service. “Serverless” might sound misleading, as there definitely are servers involved; however, the idea is that the responsibility of server management, scaling, and infrastructure concerns are taken away from the developer. This allows us to focus on writing and deploying our code. Azure Functions will then take care of running our code in response to a wide range of events, like HTTP requests, database operations, or time-based triggers.

The way Azure Functions works is fascinating. Once your function is deployed, Azure sets up an environment for it to run. When the specific event that your function listens to happens, Azure Functions wakes up, executes your function, and then goes back to sleep. This “on-demand” nature ensures that you’re only billed for the actual execution time of your function, rather than any idle time.

Now, you might wonder how Azure Functions stands apart from Azure AppService. While both are capable of running applications, AppService is more about hosting web apps, RESTful APIs, and mobile backends. It gives you a more continuous and traditional web hosting environment. Azure Functions, on the other hand, shines when you have separate pieces of code that need to be run in reaction to certain events. Instead of continuously running and waiting for events, Functions are more about instantaneous, event-driven executions.

Given its event-driven nature, it’s no surprise that Azure Functions are often chosen for Microservices and Event-Driven Architectures. Microservices revolve around breaking an application into small, independent services. Each of these services can be developed, deployed, and scaled independently. Azure Functions’ model fits this like a glove. Similarly, in event-driven architectures, where the flow of the program is determined by events like user actions, system signals, or external triggers, Azure Functions provides a streamlined way to handle these individual events.

For the polyglots out there, the good news is that Azure Functions isn’t restricted to just one or two languages. It supports a plethora of them, including C#, Java, JavaScript, TypeScript, and Python, to name a few.

While the cloud offers many advantages, it’s immportant for developers to have the ability to test and debug their code locally. Azure Functions doesn’t disappoint here. With the Azure Functions Core Tools and the respective extensions for popular IDEs, you can seamlessly develop, test, and debug your functions on your local machine before deploying them to Azure.

Let’s get our hands slightly dirty with a detailed C# example. Imagine we want a function to greet a user:

using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

public static class GreetFunction
{
    [FunctionName("GreetUser")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("Received a greeting request.");

        string name = req.Query["name"];

        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please provide a name in the query string or in the request body.");
    }
}

In this example, we’ve defined an Azure Function that triggers on HTTP GET or POST requests. When invoked, it fetches the user’s name from the query string or request body and sends a greeting in return. If the name isn’t provided, it sends a bad request response.

Azure Functions offers an incredibly powerful, yet flexible, platform for event-driven programming in the cloud. With its ability to abstract away infrastructure concerns, support for a wide array of languages, and deep integration with the broader Azure ecosystem, it’s no wonder that developers are flocking to it for their serverless needs.

Suleyman Cabir Ataman, PhD

Architectural Design Patterns 1 – Layered (or Tiered) Pattern

Here in this first post of the blog, I want to also start with the first part of a series. Read more

Architectural Design Patterns 2 – Model-View-Controller (MVC)

When you're diving into the world of software design, you'll often hear about the Model-View-Controller, or MVC for short. It's Read more

Azure Blob Storage and C# Integration

Azure Blob Storage is one of Microsoft Azure's storage solutions. Think of it as a massive cupboard in the cloud Read more

Architectural Design Patterns 3 – Microservices

In this article, we will be talking about Microservices, a design pattern that has gained attraction for its approach to Read more

Sharing on social media:

Leave a Reply

Your email address will not be published. Required fields are marked *