The Basics of Azure Functions
I started getting back into writing a Discord Bot using a Micro-services architecture. I’m using Azure Functions to do a lot of the heavy lifting when it comes to writing my bot and I thought it might be useful to do a deep dive into why I chose them. In this post I’m going to go through what Azure Functions are, when to use them and some application structures that best leverage this technology.
What are Azure Functions
At their core Azure Functions is self-contained code, initiated with a trigger, that sometimes generate an output. From that perspective, you don’t need to manage any infrastructure or maintain always running servers. A trigger fires, your Function loads up, processes the event, then unloads all independently.
Why use Azure Functions
There are a number of benefits in using something like Azure Functions to construct your application. The primary ones are:
- No need to manage any infrastructure
- They automatically scale to handle large volumes then scale down when finished
- There are a wide variety of triggers that need minimal configuration to use
- You can reuse Function code to compose new services
There are also plenty of situations when it’s not a good idea to run Azure Functions, including:
- If you need to keep track of complex states
- If your services run for a long time
- If you have more traditional workloads, such as hosting web pages
How to use them
So now you have a good idea about what Azure Functions are, how do you go about using them? It really comes down to picking a trigger. These can really be grouped into three different types:
- Message Triggers: Such as Event Grid, Service Bus and Storage Queues. A message is generated, put into one of these services and a Function will process it.
- Storage Triggers: Such as Blob Storage, Cosmos DB etc. An event is trigger when a new or updated item in storage is generated. The Function will trigger and do something around that item.
- Timer Trigger: There really is only one of these and it will basically trigger your Function at a certain time or interval
- HTTP Trigger: This is the catch-all trigger that allows you to send a JSON payload to a HTTP/HTTPS endpoint for the Function to process.
There are a few other things to keep in mind when working with Azure Functions. They are:
- Always assume your Function is Stateless and it should always write to stateful information.
- Use static clients (https://docs.microsoft.com/en-us/azure/azure-functions/manage-connections) except for Storage to better manage connections.
- If your Function(s) get a lot of work over a long period it may be worth switching to a Dedicated App Service plan.
So there you have a basic guide to Azure Functions. I’ve included some further reference and reading to help you out. Go out and build !!