Azure Functions Webhook Interface
In November, I gave a talk at Live! 360 on how to create a basic webhook consumer using Azure Functions. This blog post will recap that talk and distill things so that you will understand the basics of Azure Function, and extend the framework solution found on Github.
What are Webhooks?
Webhooks are great little things provided by many popular services including SparkPost, Slack, Visual Studio Team Services, Office 365, Facebook, PayPal, and Salesforce. Webhooks post data based on an event to an endpoint you define.
Why serverless functions?
Serverless functions are a great innovation to help rapidly deploy solutions while reducing the overhead for organizations. The lack of hardware to maintain is a great benefit, and the serverless functions are able to handle unpredictable traffic flows. They are easy to deploy and update, so you can get up and running quickly.
Combining webhooks and serverless functions make it very easy to create rich ecosystems for automation or user interaction. Being able to drive off the events and data generated by all of these disparate systems removes the need for complicated middleware while making it very easy to incorporate custom code and events.
Azure Functions Basics
Azure Functions can be created through the Azure Console or Visual Studio. I recommend that you give both a try so you are familiar with the experiences. One of the nice things about creating something in the Azure Console is that you can download the resulting Azure Function as a Visual Studio solution file. Visual Studio is the same familiar strong IDE experience that you know and love.
There are advantages to both methods. The Azure Function console gives you direct access to control the parameters of the function’s operation from resources available to month usage limits for cost control. All of these options can be set an manipulated from Visual Studio through the host.json file and environment variables.
Creating a Basic Webhook Consumer
Create a New Visual Studio solution, and add a New Project to that solution.
Right-click the project node, and add a New Item. Choose Azure Function.
Now you have a very familiar Visual Studio project. You should have a template for a basic C# Azure Function. Time to build some code to consume your webhook.
Debug locally. That’s right, you can debug this thing locally with all the familiar Visual Studio debugging and introspection tools. Pay attention to the debugging console, as it contains a lot of valuable information about every call that you make to your function during local testing.
Publish the Azure Function. Right-click the project node and select Publish. Choose Azure Function and Create New. Note that you could update an existing function as well. The function will now appear in your Azure console.
The most common pitfall that strikes people when working with webhook consumption and serverless functions is that the function runs too long. This either causes the function to become very costly or fail entirely because of the webhook POST times out. There are a few things you can do to mediate these issues.
Webhook consumers should run asynchronously. The data should be ingested as quickly as possible and then processed. The common design mistake is trying to process the data in real time as it comes in. This works as long as the data is a consistent and small size, but if the data size can increase or be inconsistent, then it is best to ensure that the data is received and the HTTP request responded to so that timeouts do not occur.
Another thing that can help mitigate long-running processes is to store the posted data and use the serverless function to start a containerized process using something like Azure Container Services (AKS) to handle the long-running parts. Using this design, the serverless function should fire and forget the container, letting the container post its results either to a log or some other notification service of your choice. This keeps the serverless function as brief as possible while still allowing complicated processing to occur.
Let’s Light This Candle
There you have it. Now you can go forth and create your own rich ecosystem using serverless functions and webhooks. Below are a list of other resources to help you dive deeper into Azure Functions.
ps – below are some additional resources on Azure functions that you might find interesting – enjoy!
- Create your first function in the Azure portal
- Create your first function using Visual Studio
- host.json reference for Azure Functions
- Azure Resource Manager Overview
- What are Microsoft Azure Resource Groups?
- Azure Functions Pricing
- Azure Functions Webhooks Demo