Migrating custom APIs to Azure Functions

Recently I migrated my custom Node.js API from an Azure API App to an Azure Function. So how much did I have to change exactly?

Azure API Apps

Azure API Apps are a versatile platform that you can use to deploy custom APIs to Azure. API Apps are based on Azure App Services and offer the same scalability benefits as Azure Web Apps: both for deployment and for operation. With Azure API Apps you can deploy your custom APIs written in .NET, .NET core, PHP or Node.js.

What you get with Azure API Apps is in a way a web server that passes web requests into your API. It's your responsibility however to handle these requests, the API security and transform the raw requests into objects.

Building custom APIs on Azure API Apps

Recently I built a custom API using Node.js to keep the search index for my blog up-to-date. On each update of the RSS feed, I would use an IFTTT recipe to send a request to my custom API to retrieve the latest feed and push it into the Azure Search index used by my blog.

I chose to build the API using Node.js. If you've worked with Node.js, you might know that by default your Node.js application is a process that runs and stops when finished - similarly to a Windows console application. If you want a Node.js application to be able to process web requests you have to add that capability to the application yourself. And so I did. I used Express to facilitate routing and handling requests in my custom API and built my own security measure based on an API key that would be known only to my application and my IFTTT recipe.

While the API worked, it seemed like it had nearly as much code responsible for instrumentation as the logic responsible for updating my Azure Search index.

Serverless architectures with Azure Functions

Azure Functions are another service available to you in Azure that you can use to publish an API. While they are also based on Azure App Services, the biggest difference, comparing to API Apps, is that each function is meant to perform a single operation - as a function in code would do.

Azure Functions come with support for function-level security and request routing. Using configuration you can specify the triggers to which the particular function should respond, like a web request or a queue message. Aside from the triggers you can specify what the output of each function should be, which can vary from a simple web response with a particular status code to sending an email. And you can even configure your function to have multiple outputs as well!

Azure Functions eliminate the need for orchestrating your APIs and allow you to focus on your API's functionality instead.

Recently I migrated my custom Node.js API application from an Azure API App to an Azure Function. So how much did I exactly had to change?

Migrating APIs from Azure API Apps to Azure Functions

A lot. I had to remove a lot from my Node.js application. Basically I had to remove everything except for the handler for the API web request. Even there I had to remove some pieces. As I mentioned before, Azure Functions offer native support for API key security so I could just enable that in the Function's security options instead of implementing it in the code myself.

From a Node.js project complete with the Express web server, my API implemented as an Azure Function shrunk down to a single file with one additional Node.js package for parsing the contents of the RSS feed. All the orchestration and configuration, required to run the Node.js API application on Azure API Apps, got replaced by the standard capabilities of Azure Functions, simplifying the overall solution design significantly.

Azure Functions for all

So with all that simplicity you might wonder if Azure Functions are the platform that you should always use when deploying APIs in Azure. They are not. Azure Functions are meant to be used for short, atomic operations. If you need complex processing, you have to support long-running operations or have complex requirements with regards to scalability, security and portability there are better solutions available in Azure that you could use instead. But when you need light-weight APIs, nano-services-like, you should definitely consider using Azure Functions.

Comments

comments powered by Disqus