About Azure functions
Tags: azure, devops, serverless
Published: 2024-01-25 21:14:42

An **Azure function** represents a simple way to run a small piece of code (functions) in Azure. It follows a **serverless architecture**, which means we don't have to worry about managing the underlying infrastructure, we will simply provide the code. The platform automatically scales based on demand and it is billed only when the code is running. It is also known as **Functions as a Service (FaaS)**.

###Supported Languages
Azure Functions supports **multiple programming languages**, including **C#**, **Python**, **Node.js**, **Java**, **PowerShell**, and **Javascript**. You can choose the language that best suits your application and preferences.

###Development environments
You can deploy Azure Functions directly from the **Azure Portal** (great for learning and experiments), **Visual Studio** (Windows and C#), [**Azure Functions Core Tools CLI**](https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Cisolated-process%2Cnode-v4%2Cpython-v2%2Chttp-trigger%2Ccontainer-apps&pivots=programming-language-csharp) with an text editor like **Visual Code** or by using Azure DevOps. Continuous integration and deployment (CI/CD) practices can be implemented for efficient development workflows.

###Hosting choices
**Azure Functions** provides various hosting options to meet different requirements. The primary hosting choices are:

- **Consumption Plan**

The **Consumption Plan** is the default and most common hosting option for Azure Functions. In this model, **you pay for the compute resources consumed by your functions on a per-execution basis**. It automatically scales out or in based on the number of incoming events, making it well-suited for **sporadic workloads with variable demand**. This is the serverless aspect of Azure Functions, as you don't need to manage the underlying infrastructure.

- **Premium Plan**

The **Premium Plan** extends the capabilities of the Consumption Plan by providing additional features such as longer execution timeouts, virtual network integration, and support for virtual applications. It is **designed for scenarios where functions require more resources or need to run continuously**. The Premium Plan is suitable for applications with more predictable and consistent workloads.

- **App Service Plan**

**Azure Functions** can also be hosted on an App Service Plan, which is shared with other Azure App Service resources like web apps and API apps. In this hosting option, you explicitly provision and manage the underlying compute resources, and you pay for the reserved capacity. This approach **is suitable for scenarios where you need more control over the environment**, such as dedicated resources or specific virtual machine configurations.

###Triggers
A **trigger** represents what makes an Azure function to be executed. Every function has only one trigger. Common triggers include **HTTP requests (APIs and webhooks)**, **timer-based (scheduled tasks)**, **changes in Azure Storage** or **messages in Azure Service Bus** (data operation), and more.

###Input and Output bindings
**Input (read access from external service) and output bindings (write data to an external service)** in Azure Functions provide a declarative way to connect your function code to external services or data sources. A function can have multiple input and output binding but in the same time they are optional. Bindings simplify the code required to interact with these resources, allowing you to focus on the business logic of your functions. Azure Functions supports a variety of bindings for input and output scenarios. 
####Examples of Input Binding:

- **Blob Storage Binding**: Allows you to read data from or write data to Azure Blob Storage. You can specify a trigger or input binding to automatically pass data from a blob to your function.

- **Cosmos DB Binding**: Integrates with Azure Cosmos DB, allowing you to perform operations like reading documents from or writing documents to a Cosmos DB collection.

- **SignalR Binding**: Provides support for real-time communication using Azure SignalR Service. Functions can send messages to SignalR clients using this binding.

####Examples of Output Binding:

- **Blob Storage Binding**: Similar to input bindings, you can use output bindings to write data to Azure Blob Storage.

- **SendGrid Binding**: Integrates with SendGrid, a cloud-based email service, allowing functions to send emails.

- **Service Bus Binding**: Functions can use this binding to send messages to an Azure Service Bus queue or topic.

These bindings help in reducing boilerplate code and simplify the integration of Azure Functions with various services. They are configured using attributes or configuration files, making it easy to set up and manage the connections between your functions and external resources.

###Durable functions
**Durable Functions** is an extension to Azure Functions that enables building **stateful **workflows (orchestrations). It allows you to define workflows with multiple functions, manage state, and handle long-running processes.

####Function types
You can use three durable function types:
 
- **Client**: Client functions are the entry point for creating an instance of a Durable Functions orchestration. They can run in response to an event from many sources, such as a new HTTP request arriving, a message being posted to a message queue, an event arriving in an event stream. You can write them in any of the supported languages.

- **Orchestrator**: Orchestrator functions define the workflow's control flow and can coordinate the execution of multiple activity functions. They are responsible for making decisions, handling errors, and managing the state of the workflow. Orchestrator functions are long-running and stateful.

- **Activity**: Activity functions perform specific units of work within a workflow. They are short-lived, stateless functions that carry out tasks such as data processing, external API calls, or computations. Orchestrator functions can call activity functions to accomplish various steps in the workflow.

**Durable Functions** are particularly useful for scenarios where you need to manage long-running processes, deal with complex workflows, or coordinate interactions between multiple services. They provide an abstraction layer that simplifies the development of reliable and scalable stateful workflows in a serverless environment.

###Custom handlers
Azure Functions allows you to use **custom handlers** to extend the behavior of your functions by interacting with the underlying execution environment. Custom handlers provide more control and flexibility over the execution process and enable you to customize aspects of function execution, such as handling HTTP requests or integrating with specific runtimes.
Custom handlers are best suited for situations where you want to:
Implement a function app in a language that's not currently offered out-of-the box, such as Go or Rust.
Implement a function app in a runtime that's not currently featured by default, such as Deno.

###Security considerations
Securing Azure Functions is crucial to ensure the confidentiality, integrity, and availability of your applications and data. Here are some security considerations and best practices for Azure Functions:

- **HTTPS/TLS Encryption**: always use HTTPS to secure data in transit. Azure Functions provides built-in support for HTTPS, and you can configure custom domains with SSL/TLS certificates to ensure secure communication
- **Function Authentication Key**: when using HTTP-triggered functions, consider using function authentication keys to secure access. These keys act as secrets and should be treated with care. Rotate keys periodically, and avoid hardcoding them in client applications.

###Local development and monitoring and logging
**Azure Functions** supports local development through tools like Azure Functions Core Tools, enabling you to test and debug functions on your local machine before deploying them to the cloud. Azure Functions offers built-in logging and monitoring capabilities. You can use Azure Application Insights or other monitoring solutions to gain insights into the performance and behavior of your functions.

Understanding these aspects of **Azure Functions** will help you make informed decisions when designing, developing, and deploying serverless applications on the Azure platform.

Photo by <a href="https://unsplash.com/@growtika?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Growtika</a> on <a href="https://unsplash.com/photos/chart-radar-chart-qPkdgA-KDik?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Unsplash</a>