Azure Functions Core Concepts
Azure Functions is a serverless compute service that enables you to run code on demand without explicitly provisioning or managing infrastructure. It's a powerful way to build event-driven applications and microservices. Understanding its core concepts is crucial for effective development.
Functions
A function is the fundamental unit of computation in Azure Functions. It's a piece of code that executes in response to an event. You can write functions in various programming languages, including C#, JavaScript, Python, Java, and PowerShell.
- Each function is triggered by a specific event.
- Functions are stateless by default, meaning they don't retain information between executions unless explicitly managed.
- You can group related functions into a function app.
Function App
A function app is the logical container for your individual functions. It allows you to manage, deploy, and monitor your functions as a single unit. Function apps share the same hosting plan and execution environment.
- All functions within a function app share configurations like connection strings and environment variables.
- Function apps can be hosted on different plans, such as Consumption, Premium, or App Service plans, each offering different scaling and pricing characteristics.
Triggers
A trigger defines how a function is invoked. It's the event that causes your function code to run. Azure Functions supports a wide variety of triggers, including:
- HTTP Trigger: Invoked via an HTTP request.
- Timer Trigger: Executes on a schedule (e.g., every hour).
- Blob Trigger: Runs when a new or updated blob is detected in Azure Blob Storage.
- Queue Trigger: Executes when a message arrives in an Azure Storage Queue.
- Event Hub Trigger: Responds to events from Azure Event Hubs.
- Service Bus Trigger: Reacts to messages in Azure Service Bus queues or topics.
Each trigger has its own configuration, specifying the source and conditions for invocation.
Bindings
Bindings provide a declarative way to connect to other Azure services or external data sources without writing custom integration code. They simplify data input and output for your functions.
- Input Bindings: Bring data into your function. For example, an input binding can fetch a document from a database based on a trigger parameter.
- Output Bindings: Send data from your function to another service. For instance, an output binding can write a message to an Azure Storage Queue.
Bindings are configured in the function.json file or through attributes in your code, abstracting away the complexities of SDKs and APIs.
Here's an example of a function with an HTTP trigger and an output binding:
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "blob",
      "direction": "out",
      "name": "outputBlob",
      "path": "output-container/{rand-guid}.txt",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
            Hosting Plans
Azure Functions offers several hosting plans to suit different needs and budgets:
- Consumption Plan: Pay only for the time your code runs. Scales automatically based on demand, including scaling to zero. Ideal for event-driven workloads with variable traffic.
- Premium Plan: Offers pre-warmed instances for zero-cold-start execution, VNet connectivity, and longer runtimes. A good balance between cost and performance for demanding applications.
- App Service Plan: Runs your functions on existing App Service virtual machines. Provides predictable costs and allows you to run functions alongside other App Service applications.
Key Takeaway
Azure Functions allows you to build event-driven applications by composing functions that are triggered by various events and interact with other services via bindings, all managed within a function app and hosted on flexible hosting plans.