Spinning up 'Severless' Functions in Azure

Since I started using Azure App Services in 2016, Microsoft has seriously pushed their cloud technology, there is an absolutely massive stack to get to know. There's everything from web servers, to SQL servers, to data science toolboxes (using Jupyter Notebooks), cloud based network infrastructure, and the list goes on and on.

One of the most productive tools that we've been using over the last few years has been Azure Functions, which are Microsoft's answer to AWS serverless functions. You can write Azure functions in a number of language-frameworks, including Node.js in VS Code, but today we'll cover writing these in C# out of Visual Studio 2019.

The Basics

If you're unfamiliar with the concept of a 'severless' function, it's basically a way for you to rent a few minutes of computation time to perform a simple task. We use these to pull data from a software system, process it, and store it in our data warehouse. Conversely, we pull data out of the data warehouse with Azure Functions, and use the data to sync up two discrete software systems.

The underlying technology here is Asp.NET Core, so if you're familiar with Asp, or Azure App Services, a lot of this will be familiar to you.

Time & Atomicity

Azure functions can only run for an upper limit of 10 minutes in production, so you'll need to make sure your task can be completed in 10 minutes. Further, should the function not complete for any reason, you'll need to start over. It's important that you don't write any data to your database unless the function has run to completion. This 'all-or-nothing' approach is extremely important, because the app service may stop mid-task without warning, and the last thing you want is a half-completed update task in a production database (what a mess!).

With the Mongo libraries, for example, you'd want to use a bulk upsert wrapped in a transaction. Process the data first, then when ready, update the database. If the function app service dies in the middle of the task, nothing is written. With Transact-SQL, the approach is roughly the same, make sure your write-operation is wrapped in a transaction, and the process becomes atomic.

Getting Started

Getting going in visual studio is quite easy, you'll just want to create a new Azure Function from the project template.

The first choice to make here is how you'd like to trigger your function app. Our Azure Functions serve to automate business process and sync data from one system to another, as well as pull data for analytics. So for our purposes timer triggers will do just fine, however you can trigger these via HTTP, queue services, or a number of other approaches.

As a side note, SignalR is a framework connecting web applications to a backend without requiring the page to refresh, so it's interesting to see that we can trigger Azure Functions with it too.

Writing the Function App

Once you've setup the App (Visual Studio has done most of the work for us, which is always great for small teams like mine), you'll a few things already there for you.

First is the Cron timer used to trigger the app, the default one runs every 5 minutes, but you can make it run on any time cycle you can think of. The first of every month, every second Tuesday, once a day at midnight etc.  

 
You'll also notice that there's some default log information being written out to console here. This is how you'll push information out to your Azure Function console, which is very important for debugging. You can get a logstream in Azure, which is super useful if you need to see the function running in test or in prod.

Here it is running in my dev environment. Easy!
 

 

 Final Notes

Getting an Azure Function up and running takes 5 minutes. It's only a matter of just writing the code to do what you want to do. In my experience it's quite limited scope regardless of what you need it to do. To further multiply your capacity, you can use Azure's GitHub integration to setup Continuous Integration. Just point Azure at your production branch, and your Function App Service will see when you've pushed a new revision upstream.

I'll cover this in more detail next week, since CI in Azure is a bit broader in scope, and is useful beyond just Azure Functions.

 

Comments

Popular posts from this blog

Creating HID Override Layouts for the Unity Input System

Building Ben Eater's 8-bit CPU Clock (part 1)

Managing API Keys for Serverless Functions with Azure KeyVault