Wednesday, 24 September 2025

Trigger in Azure Functions

🟢 What is a Trigger in Azure Functions?

  • An Azure Function is a small piece of code that runs only when something happens.

  • That “something” is called a trigger.

  • Trigger = automatic starter for your function.

Example:
If you want a function to run every day at 9 AM, a Timer trigger starts it for you.


🧩 Key Idea

  • Each function must have exactly one trigger.

  • The trigger decides:

    1. When the function runs.

    2. What input data is passed to it.


🔑 Common Trigger Types (with Examples)

Below are the most used Azure Function triggers and what they do.

Trigger TypeWhat Starts the FunctionSimple Example
HTTP TriggerAn HTTP request (like a web API call)Someone sends a GET/POST request to a URL such as https://myfunc.azurewebsites.net/api/hello
Timer TriggerA scheduled time (like a cron job)Run cleanup every night at 2 AM
Blob TriggerA file is added or updated in Azure Blob StorageAutomatically resize an image after it’s uploaded
Queue Storage TriggerA new message is put in an Azure Storage QueueProcess new orders added to a queue
Service Bus TriggerA message arrives in an Azure Service Bus queue or topicHandle incoming payment notifications
Event Grid TriggerAn Azure event occurs (e.g., new file in a storage account, resource change)Send a Slack message when a new file arrives
Event Hub TriggerA batch of event-stream data arrives (IoT or telemetry)Analyze live sensor data
Cosmos DB TriggerA document changes in a Cosmos DB collectionUpdate a cache or run analytics whenever a record changes
SignalR Trigger (rare)A SignalR event happensSend real-time messages to connected clients

🛠️ How It Works Step by Step

  1. Binding in Code
    In the function’s code you specify the trigger type and connection details.

    [FunctionName("MyHttpFunction")] public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req, ILogger log) { log.LogInformation("HTTP trigger fired."); return new OkObjectResult("Hello World!"); }
  2. Runtime Listens
    Azure Functions runtime listens for that event (HTTP call, queue message, timer schedule, etc.).

  3. Event Happens
    When the event occurs, the runtime starts the function automatically and passes in the data (like the HTTP body, message text, or blob contents).


⚙️ Details About Each Trigger

1. HTTP Trigger

  • Use for: APIs or webhooks.

  • Input: Request body, query strings, headers.

  • Return: HTTP response.

  • Security: Set AuthorizationLevel (Anonymous, Function, Admin).

2. Timer Trigger

  • Use for: Scheduled tasks like cleanup, backups.

  • Schedule Format: CRON expression, e.g., "0 0 2 * * *" (2 AM daily).

  • Example: Nightly email reports.

3. Blob Trigger

  • Use for: Processing files.

  • Works with: Azure Blob Storage containers.

  • Example: Process a CSV when someone uploads it.

4. Queue Storage Trigger

  • Use for: Simple messaging between services.

  • Example: One system drops a message when an order is placed; the function picks it up and processes it.

5. Service Bus Trigger

  • Use for: Enterprise messaging (queues or topics).

  • Example: Payment gateway sends a Service Bus message when a payment succeeds.

6. Event Grid Trigger

  • Use for: Reacting to Azure events in near real-time.

  • Example: Notify a team when a new blob is created.

7. Event Hub Trigger

  • Use for: High-volume event streams like IoT telemetry.

  • Example: Analyze millions of device signals per minute.

8. Cosmos DB Trigger

  • Use for: Reacting to database changes.

  • Example: Automatically update a search index when a product record changes.


🧠 Best Practices

  1. One Trigger per Function

    • Each function listens to exactly one event source.

  2. Keep Functions Short

    • Do small tasks quickly to avoid timeouts.

  3. Scale Automatically

    • Triggers like Queue, Event Hub, and Blob scale the number of function instances based on event volume.

  4. Use Bindings for Input/Output

    • You can directly read or write to storage, queues, etc., with simple parameters—no extra code.


Quick Recap

  • Trigger = automatic starter for your code.

  • Common triggers: HTTP, Timer, Blob, Queue, Service Bus, Event Grid, Event Hub, Cosmos DB.

  • Each function must have exactly one trigger.

  • Azure Functions automatically listens for the event and runs your code when it happens.

In short:
Azure Function triggers let you run code automatically whenever a specific event (like a request, message, or file upload) occurs—no servers to manage, just write the function and choose the trigger.