Azure Functions Serverless Samples

Explore a curated collection of code samples demonstrating the power and versatility of Azure Functions for serverless computing. These examples cover a wide range of scenarios, from simple HTTP triggers to complex integrations with other Azure services.

HTTP Triggered Function

A basic Azure Function that responds to HTTP requests. Perfect for building simple web APIs or webhook endpoints.

View Details Download Sample

Blob Storage Triggered Function

This function automatically executes when a new blob is created or updated in Azure Blob Storage. Ideal for image processing, data transformation, or file validation.

View Details Download Sample

Queue Storage Triggered Function

Processes messages from an Azure Queue Storage queue. Useful for asynchronous task processing, decoupling applications, and handling background jobs.

View Details Download Sample

Cosmos DB Triggered Function

Reacts to changes in an Azure Cosmos DB container. Enables real-time data processing and event-driven architectures with NoSQL databases.

View Details Download Sample

Event Hubs Triggered Function

Processes events from Azure Event Hubs, a big data streaming platform. Essential for IoT data ingestion, real-time analytics, and telemetry processing.

View Details Download Sample

HTTP Triggered Function

Description

This sample demonstrates a fundamental Azure Function that can be triggered via HTTP requests. It shows how to handle incoming request data, process it, and return a response. This is the most common entry point for web-based serverless applications.

Code Snippet (JavaScript)


async function index(context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.res = {
        status: 200,
        body: responseMessage
    };
}
            

Languages Supported

Learn More

Azure Functions HTTP Trigger Documentation

Blob Storage Triggered Function

Description

This sample showcases how to create an Azure Function that automatically runs whenever a new file is uploaded to a specified Azure Blob Storage container. It's often used for post-processing, data validation, or generating thumbnails.

Code Snippet (C#)


using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.IO;

public static class BlobTriggerFunction
{
    [FunctionName("BlobTriggerCSharp")]
    public static void Run(
        [BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob,
        string name,
        ILogger log)
    {
        log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    }
}
            

Languages Supported

Learn More

Azure Functions Blob Storage Trigger Documentation

Queue Storage Triggered Function

Description

This sample demonstrates processing messages from an Azure Queue Storage queue. When a message is added to the queue, this function will be triggered to process it, making it suitable for asynchronous operations.

Code Snippet (Python)


import logging
import azure.functions as func

def main(msg: func.QueueMessage) -> None:
    logging.info('Python queue trigger function processed a queue item: %s',
                 msg.get_body().decode('utf-8'))
            

Languages Supported

Learn More

Azure Functions Queue Storage Trigger Documentation

Cosmos DB Triggered Function

Description

This sample shows how to create an Azure Function that reacts to changes in an Azure Cosmos DB document. It's powerful for implementing real-time data synchronization, auditing, or triggering downstream processes based on database events.

Code Snippet (JavaScript)


module.exports = async function (context, documents) {
    context.log('JavaScript Cosmos DB trigger function was triggered by', documents.length, 'documents');
    for (const doc of documents) {
        context.log('Processing document:', doc);
        // Add your processing logic here
    }
};
            

Languages Supported

Learn More

Azure Functions Cosmos DB Trigger Documentation

Event Hubs Triggered Function

Description

This sample demonstrates processing events from Azure Event Hubs. It's designed for high-throughput scenarios, such as ingesting data from IoT devices, application logs, or clickstream data.

Code Snippet (C#)


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.EventHubs;
using Microsoft.Extensions.Logging;

public static class EventHubTriggerFunction
{
    [FunctionName("EventHubTriggerCSharp")]
    public static void Run(
        [EventHubTrigger("samples-workitems", Connection = "EventHubConnectionString")] EventData[] events,
        ILogger log)
    {
        log.LogInformation($"C# Event Hub trigger function executed at: {DateTime.Now}");
        foreach (EventData eventData in events)
        {
            log.LogInformation($"Receieved event: {Encoding.UTF8.GetString(eventData.Body.ToArray())}");
        }
    }
}
            

Languages Supported

Learn More

Azure Functions Event Hubs Trigger Documentation