Azure Functions Triggers and Bindings

Last updated: October 26, 2023

On this page

Introduction

Azure Functions provides a powerful and flexible way to build serverless applications. At its core, Azure Functions relies on triggers and bindings to connect your code to other Azure services, as well as to on-premises or other cloud services.

A trigger defines how a function is executed. It's what causes your function to run. A function must have exactly one trigger.

Bindings allow you to connect your function to data sources and other services without explicitly writing client code to manage them. Bindings declare these connections, making your code cleaner and easier to manage.

Trigger Types

Triggers can be categorized based on how they initiate function execution:

Binding Types

Bindings can be classified into two main categories:

A single function can have multiple input and output bindings, in addition to its trigger.

Declarative Bindings

Bindings are declarative, meaning you define them in your function's configuration file (e.g., function.json for JavaScript/C#/etc., or attributes for C#/.NET). This separation of concerns makes your code more readable and maintainable.

Binding Syntax

The syntax for defining bindings varies slightly depending on the language and runtime. However, the core concept involves specifying the type of binding, the direction (input, output, or inout), and other relevant properties like path, connection, or queueName.

Example function.json

Here's a common example of a function.json file for an HTTP-triggered function that reads from a blob:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "blob",
      "direction": "in",
      "name": "myBlob",
      "path": "input-container/{name}.txt",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
        

Common Bindings

Azure Functions supports a wide range of bindings for various services. Some of the most common include:

Trigger Examples

HTTP Trigger

Invoked by an HTTP request.

{
  "type": "httpTrigger",
  "name": "req",
  "direction": "in",
  "methods": ["get", "post"]
}
        

Timer Trigger

Runs on a specified schedule.

{
  "type": "timerTrigger",
  "name": "myTimer",
  "direction": "in",
  "schedule": "0 */5 * * * *"
}
        

Blob Trigger

Triggered when a blob is created or updated.

{
  "type": "blobTrigger",
  "name": "myBlob",
  "direction": "in",
  "path": "samples-workitems/{name}.json"
}
        

Binding Configuration

Bindings are configured in the function.json file (or using attributes in C#). Each binding definition requires at least a type, direction, and a name, which is how the binding is referenced in your code.

Common Configuration Properties:

Property Description
type The type of the binding (e.g., httpTrigger, blob, queue).
direction The direction of the binding: in, out, or inout.
name The name of the parameter in your function code that the binding maps to.
path For blob and file bindings, the path to the blob or file. Can include patterns.
connection The name of the app setting that contains the connection string for the service.
queueName For queue bindings, the name of the queue.
schedule For timer triggers, the CRON expression defining the schedule.

Advanced Topics

Binding Expressions

Binding expressions allow you to dynamically reference properties of the trigger event or other bindings. For example, in a blob trigger, you can use {name} to refer to the blob's name in the path property of another binding.

Custom Bindings

For services not covered by built-in bindings, you can create your own custom bindings.

Tip: Always ensure your connection strings are securely stored in application settings, not directly in your function.json or code.