Azure Functions | An overview | Part 1
Azure Functions is a serverless compute service that enables you to run event-triggered code without having to explicitly provision or manage infrastructure. It is a perfect fit for a variety of application scenarios, such as handling incoming webhooks, running background jobs, or processing data streams.
One of the main benefits of Azure Functions is its scalability. When an event occurs that triggers your function, Azure automatically provisions the necessary resources to run your code, and then scales them back down when the function completes. This means that you only pay for the compute resources that you actually use, and you don't have to worry about over-provisioning or under-provisioning resources.
You can write functions in a variety of languages, including C#, F#, JavaScript, Python, and Java. Additionally, you can use a variety of triggers and bindings to integrate with other Azure services, such as Event Grid, Service Bus, and Cosmos DB.
Here is a simple example of an Azure Function written in C# that listens for an HTTP request and returns a "Hello, World!" message:
This example uses the HttpTrigger attribute to listen for incoming HTTP requests, and the FunctionName attribute to give the function a name. The function takes an HttpRequestMessage parameter, which represents the incoming request, and an ILogger parameter, which can be used to log information about the function's execution.
When the function is triggered, it logs a message, and then it creates and returns an HttpResponseMessage containing the "Hello, World!" message.
Bindings
Azure Functions bindings are a way for Azure Functions to connect to other Azure services and to external services, without having to write the code for connection and data transfer explicitly. They are a way to declaratively connect your function to different data sources and services, such as Azure Storage, Azure Event Hubs, Azure Service Bus, and many others.
There are two types of bindings: Input bindings are used to bring data into your function, while output bindings are used to send data out of your function.
Here is an animated example that demonstrates triggers, input and output bindings:
There are several built-in bindings available, such as:
- Blob storage binding to read and write data to Azure Blob storage
- Queue storage binding to read and write data to Azure Queue storage
- Table storage binding to read and write data to Azure Table storage
- Cosmos DB binding to read and write data to Cosmos DB
- Event Grid binding to handle events from Azure Event Grid
- Service Bus binding to send and receive messages from Azure Service Bus
- HTTP and WebHooks bindings to handle HTTP requests and responses
- Timer binding to schedule function execution.
On the Microsoft documentation you can find the list of all supported bindings, and there is a possibility to create custom input and output bindings, explained in details at the Microsoft’s guidelines.
Here's an example of an Azure Function with input and output bindings for Cosmos DB in C#:
In this example the function uses the CosmosDBTrigger attribute to define an input binding that listens to changes in a specific Cosmos DB database and collection. The input parameter is defined as an IEnumerable<dynamic>, which allows the function to receive a collection of items that were added, modified, or deleted in the specified Cosmos DB collection.
It also uses the CosmosDB attribute to define an output binding that writes data to the same Cosmos DB database and collection. The output parameter is defined as an IAsyncCollector<dynamic>, which allows the function to add, update, or delete items in the specified Cosmos DB collection.
The simple code does iteration through the items received in the input binding and performs some processing on each item. Then it creates the item in the cosmosDb if doesn’t exist, as specified in the output binding attribute.
Durable Functions
Azure Durable Functions is an extension of Azure Functions that enables you to write and manage stateful, long-running, and reliable multi-step serverless workflows using the familiar programming model of Azure Functions.
It also provides built-in support for persistence, management, and scaling of workflows, enabling you to build highly available, long-running, and fault-tolerant workflows.
Here's an example of a simple Durable Function that runs two other functions (called activities) in parallel and then waits for both to complete before returning the results:
Azure Durable Functions are a good chose for the following use cases:
- Workflow automation for automating complex, multi-step business processes, such as order processing, fulfillment, and invoicing. It allows you to express complex workflows, and it provides built-in support for persistence, management, and scaling.
- Event-driven architectures that respond to events from various sources, such as IoT devices, mobile apps, or webhooks. It allows you to build complex event-processing pipelines that can handle high volumes of events, and it provides built-in support for fault-tolerance and scalability.
- Long-running tasks such as data processing or machine learning, in a serverless and cost-effective way. It allows you to break down long-running tasks into smaller, stateless functions that can be executed in parallel, and it provides built-in support for progress tracking, error handling, and scaling.
- Human-interactive workflows such as approvals or reviews, that involve human interaction. It allows you to express complex, human-interactive workflows.
- Coordination and scheduling activities and workflows. It allows you to easily schedule and run activities and workflows based on a schedule or external events, and to express complex coordination patterns such as fan-out/fan-in, human-interaction, etc.
Considerations and when to avoid Azure Functions
It is important to evaluate the specific requirements of each project and choose the most suitable tools and services for your need. Azure Functions is a powerful and flexible service that can be used for a wide variety of use cases, but it may not be the best option for every scenario.
Here are a few considerations on when not to use Azure Functions:
- High-performance computing that require low-latency and high-throughput processing,
- Large-scale data processing scenarios that require data processing in parallel, such as big data processing or real-time analytics.
- Applications that require a significant amount of resources for a long period of time.
- Mission-critical applications that require advanced disaster recovery and high-availability capabilities. In this cases a dedicated host or an isolated virtual machine is a safer choice.
- On-premises or disconnected environments. While Azure Functions is available on Azure Stack, it is not yet available to run on-premises or disconnected environments. In such scenarios, you may want to consider using Azure Container Instances or Azure IoT Edge.
Summary
Using Serverless Azure Functions we leverage the cloud in an efficient and cost effective way, using simple, pay-per-execution model. With Azure Functions, developers can easily build and deploy scalable, event-driven applications that can respond to changing business needs, without the need to manage infrastructure.
In the next part of this blog post we will write about successful stories and demonstrate the capabilities of Azure Functions to work with other Azure services in a serverless, cost-effective and scalable way.