Azure Functions Triggers and Bindings

This article provides a comprehensive overview of triggers and bindings in Azure Functions, explaining how they simplify serverless development by abstracting away complex integrations.

What are Triggers and Bindings?

Azure Functions uses a declarative programming model to connect your function code to other Azure services and external event sources. This is achieved through two core concepts:

How They Work Together

A function always has exactly one trigger, but can have multiple input and output bindings. The trigger defines the event that initiates execution, and the bindings define how data flows into and out of your function. This model drastically reduces boilerplate code and allows you to focus on your core business logic.

Common Triggers

Azure Functions supports a wide variety of triggers, allowing you to build event-driven applications across many services. Some of the most common include:

Common Bindings

Bindings enable seamless integration with various Azure and third-party services. They can be used for both input and output:

Input Bindings

Input bindings allow you to read data from a service into your function. The function parameters are bound to the data from the specified service.

Output Bindings

Output bindings allow you to write data from your function to a service. You typically return a value from your function or use an output binding parameter to send data.

Example: HTTP Trigger with Blob Input and Queue Output

Function.cs (C# Example)


using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

public class BlobProcessorFunction
{
    [Function(nameof(BlobProcessorFunction))]
    public void Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
        [BlobInput("input-container/{Query.blobname}")] string blobContent,
        [Queue("output-queue", Connection = "AzureWebJobsStorage")] out string queueMessage,
        ILogger log)
    {
        log.LogInformation($"HTTP trigger function processed a request for blob: {req.Query["blobname"]}");
        log.LogInformation($"Blob content: {blobContent}");

        // Process the blob content and prepare a message for the queue
        queueMessage = $"Processed blob content: {blobContent.ToUpper()}";
        log.LogInformation("Message sent to output queue.");
    }
}
                

In this example:

  • The function is triggered by an HTTP request.
  • It reads the content of a blob specified in the query parameter blobname from the input-container.
  • It writes a processed message (uppercase blob content) to the output-queue.
  • The Connection = "AzureWebJobsStorage" specifies the application setting that contains the connection string for Azure Storage.

Configuring Bindings

Bindings are typically configured in the function.json file (for older programming models) or through attributes in your code (for newer programming models like .NET Worker). This configuration specifies the type of binding, the direction (input/output), and the service-specific properties like container names, queue names, or connection strings.

Important Note on Connection Strings

Connection strings for services used by bindings are usually stored in the application's configuration settings (e.g., local.settings.json for local development or application settings in Azure). Avoid hardcoding connection strings directly in your code.

Benefits of Triggers and Bindings

Further Reading