แก้ไข

Azure Functions scenarios

Often, you build systems that react to a series of critical events. Whether you're building a web API, responding to database changes, or processing event streams or messages, you can use Azure Functions to implement these systems.

In many cases, a function integrates with an array of cloud services to provide feature-rich implementations. The following list shows common (but by no means exhaustive) scenarios for Azure Functions.

Select your development language at the top of the article.

Process file uploads

You can use functions in several ways to process files into or out of a blob storage container. To learn more about options for triggering on a blob container, see Working with blobs in the best practices documentation.

For example, in a retail solution, a partner system can submit product catalog information as files into blob storage. You can use a blob-triggered function to validate, transform, and process the files into the main system as you upload them.

Diagram of a file upload process using Azure Functions.

The following tutorials use an Azure Blob trigger (Azure Event Grid based) to process files in a blob container:

For example, you can use the Blob trigger with an event subscription on blob containers:

[FunctionName("ProcessCatalogData")]
public static async Task Run([BlobTrigger("catalog-uploads/{name}", Source = BlobTriggerSource.EventGrid, Connection = "<NAMED_STORAGE_CONNECTION>")] Stream myCatalogData, string name, ILogger log)
{
    log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myCatalogData.Length} Bytes");

    using (var reader = new StreamReader(myCatalogData))
    {
        var catalogEntry = await reader.ReadLineAsync();
        while(catalogEntry !=null)
        {
            // Process the catalog entry
            // ...

            catalogEntry = await reader.ReadLineAsync();
        }
    }
}

Real-time stream and event processing

Cloud applications, IoT devices, and networking devices generate and collect a large amount of customer data. Azure Functions can process that data in near real-time as the hot path, then store it in Azure Cosmos DB for use in an analytics dashboard.

Your functions can also use low-latency event triggers, like Event Grid, and real-time outputs like SignalR to process data in near real-time.

Diagram of a real-time stream process using Azure Functions.

For example, you can use the event hubs trigger to read from an event hub and the output binding to write to an event hub after debatching and transforming the events:

[FunctionName("ProcessorFunction")]
public static async Task Run(
    [EventHubTrigger(
        "%Input_EH_Name%",
        Connection = "InputEventHubConnectionSetting",
        ConsumerGroup = "%Input_EH_ConsumerGroup%")] EventData[] inputMessages,
    [EventHub(
        "%Output_EH_Name%",
        Connection = "OutputEventHubConnectionSetting")] IAsyncCollector<SensorDataRecord> outputMessages,
    PartitionContext partitionContext,
    ILogger log)
{
    var debatcher = new Debatcher(log);
    var debatchedMessages = await debatcher.Debatch(inputMessages, partitionContext.PartitionId);

    var xformer = new Transformer(log);
    await xformer.Transform(debatchedMessages, partitionContext.PartitionId, outputMessages);
}

Build AI-enabled apps

Use Azure Functions to make data and APIs available to AI clients and to add AI reasoning to event-driven applications.

Make your data and APIs available to AI

AI clients and agents need tools that provide controlled access to business data, APIs, and application logic. You can use Azure Functions to build and host remote Model Context Protocol (MCP) servers that expose these capabilities as tools. Functions provides managed hosting, authentication, networking, monitoring, and scaling for your MCP server.

For example, you might expose product inventory, customer account data, or an existing business API as tools that an AI client can discover and call.

Diagram showing an AI client calling a remote MCP server hosted by Azure Functions, which provides controlled access to business APIs and data.

Add AI reasoning to business events

Some business events require reasoning before your application can decide what to do next. Azure Functions hosted skills let you define AI-powered work in Markdown and start it from schedules, HTTP requests, queue messages, storage changes, and other events. The hosted skill can call tools and services before returning a result or taking an action.

For example, a hosted skill can classify an incoming complaint and route it to the appropriate queue. For more complex work, a dynamic workflow can create a durable, multistep plan that runs tasks in parallel, waits for external conditions, and resumes after worker restarts.

Diagram showing events, messages, and schedules starting a hosted skill on Azure Functions, which uses a Foundry model and can call tools, APIs, connectors, and actions.

Run scheduled tasks

Functions enables you to run your code based on a cron schedule that you define.

To learn more, see Create a function in the Azure portal that runs on a schedule.

For example, you might analyze a financial services customer database for duplicate entries every 15 minutes to avoid multiple communications going out to the same customer.

Diagram of a scheduled task where a function cleans a database every 15 minutes deduplicating entries based on business logic.

For examples, see these code snippets:

[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, ILogger log)
{
    if (myTimer.IsPastDue)
    {
        log.LogInformation("Timer is running late!");
    }
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

    // Perform the database deduplication
}

Build a scalable web API

An HTTP-triggered function defines an HTTP endpoint. These endpoints run function code that can connect to other services directly or by using binding extensions. You can compose the endpoints into a web-based API.

You can also use an HTTP-triggered function endpoint as a webhook integration, such as GitHub webhooks. In this way, you can create functions that process data from GitHub events. For more information, see Azure Functions HTTP trigger.

Diagram of processing an HTTP request using Azure Functions.

For examples, see these code snippets:

[FunctionName("InsertName")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
    [CosmosDB(
        databaseName: "my-database",
        collectionName: "my-container",
        ConnectionStringSetting = "CosmosDbConnectionString")]IAsyncCollector<dynamic> documentsOut,
    ILogger log)
{
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    string name = data?.name;

    if (name == null)
    {
        return new BadRequestObjectResult("Please pass a name in the request body json");
    }

    // Add a JSON document to the output container.
    await documentsOut.AddAsync(new
    {
        // create a random ID
        id = System.Guid.NewGuid().ToString(),
        name = name
    });

    return new OkResult();
}

Build a serverless workflow

Functions often serve as the compute component in a serverless workflow topology, such as a Logic Apps workflow. You can also create long-running orchestrations by using the Durable Functions extension. For more information, see Durable Functions overview.

A combination diagram of a series of specific serverless workflows using Azure Functions.

Consider these examples:

Respond to database changes

Some processes need to log, audit, or perform other operations when stored data changes. Functions triggers provide a good way to get notified of data changes to initial such an operation.

Diagram of a function being used to respond to database changes.

Consider these examples:

Create reliable message systems

You can use Functions with Azure messaging services to create advanced event-driven messaging solutions.

For example, you can use triggers on Azure Storage queues as a way to chain together a series of function executions. Or use service bus queues and triggers for an online ordering system.

Diagram of Azure Functions in a reliable message system.

These articles show how to write output to a storage queue:

These articles show how to trigger from an Azure Service Bus queue or topic.

Next step