Azure Functions Basics

An introduction to the core concepts of Azure Functions.

What are Azure Functions?

Azure Functions is a serverless compute service that enables you to run small pieces of code, or "functions," in the cloud without the need to provision or manage infrastructure. It's event-driven, meaning functions are typically triggered by specific events such as HTTP requests, timer schedules, messages from a queue, or changes in data.

Key benefits of Azure Functions include:

  • Event-driven: Respond to a wide range of events.
  • Serverless: Focus on code, not infrastructure.
  • Scalable: Automatically scales based on demand.
  • Cost-effective: Pay only for the compute time you consume.
  • Polyglot: Supports multiple programming languages.

Key Concept: Serverless

Serverless doesn't mean there's no server; it means you, as the developer, don't manage the server. The cloud provider (Azure in this case) handles all the underlying infrastructure, patching, scaling, and maintenance.

Triggers and Bindings

Triggers and bindings are the fundamental building blocks of Azure Functions. They decouple your function code from the underlying event sources and output destinations.

Triggers

A trigger defines how a function is invoked. Each function must have exactly one trigger.

  • HTTP Trigger: Invoked by an HTTP request. Ideal for building APIs and webhooks.
  • Timer Trigger: Invoked on a schedule (e.g., every hour, daily).
  • Queue Trigger: Invoked when a new message arrives in an Azure Storage Queue.
  • Blob Trigger: Invoked when a new or updated blob is detected in Azure Blob Storage.
  • Cosmos DB Trigger: Invoked when changes are detected in a Cosmos DB collection.
  • And many more...

Bindings

Bindings allow your function to connect to other Azure services and SaaS offerings without writing explicit client code. They simplify input and output operations.

  • Input Bindings: Provide data to your function from an external service.
  • Output Bindings: Send data from your function to an external service.

Example: HTTP Trigger with Blob Input Binding

This function is triggered by an HTTP request. It then reads the content of a specific blob from Azure Blob Storage using an input binding, and returns the blob content in the HTTP response.


using System;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

public static class HttpBlobReader
{
    [FunctionName("HttpBlobReader")]
    public static IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
        [Blob("mycontainer/{name}.txt", FileAccess.Read)] Stream myBlob,
        string name,
        ILogger log)
    {
        log.LogInformation($"HTTP trigger processed a request for blob: {name}.txt");

        if (myBlob == null)
        {
            return new NotFoundResult();
        }

        using (var reader = new StreamReader(myBlob))
        {
            string content = reader.ReadToEnd();
            return new OkObjectResult($"Content of {name}.txt: \n\n{content}");
        }
    }
}
                    

Programming Model

Azure Functions supports multiple programming languages, including C#, JavaScript, TypeScript, Python, Java, PowerShell, and more. The core programming model is based on the concept of functions.

Function Structure

A typical function consists of:

  • Trigger: The event that starts the function.
  • Bindings: Connections to input and output data.
  • Function Code: The actual logic written in your chosen language.

You define your function and its bindings using attributes (in compiled languages like C#) or configuration files (like function.json for JavaScript/TypeScript/Python). The function runtime handles the execution context and passes data based on the bindings.

Tip: Choosing Your Language

Consider the familiarity of your team, the available libraries, and the performance requirements when choosing a programming language for your Azure Functions.

Deployment

Azure Functions can be deployed in several ways, offering flexibility for different development workflows.

  • Azure CLI: Use the command-line interface to deploy your function app.
  • VS Code: The Azure Functions extension for VS Code provides an integrated development and deployment experience.
  • Visual Studio: Built-in support for publishing Azure Functions projects.
  • Azure DevOps / GitHub Actions: Automate your build and deployment pipelines for CI/CD.
  • Container Deployment: Package your functions in containers for more control.

When deploying, you typically create a Function App, which is a logical container for one or more functions. This app runs on a hosting plan, such as Consumption, Premium, or App Service plans.

Important: Deployment Slots

Leverage deployment slots to test new versions of your functions with zero downtime before swapping them into production. This is crucial for safe and reliable updates.

Monitoring

Monitoring your Azure Functions is essential for understanding performance, identifying errors, and debugging issues.

  • Application Insights: Azure Functions integrates seamlessly with Application Insights for rich telemetry, logging, performance monitoring, and alerting.
  • Log Streaming: View live logs from your function app in real-time.
  • Metrics: Monitor execution count, errors, duration, and other key performance indicators through Azure Monitor.

Effective monitoring helps you ensure your functions are running as expected and proactively address any problems.

Tool Purpose Key Features
Application Insights Application performance management and monitoring Live Metrics, Trace logs, Exceptions, Dependencies, Availability tests
Azure Monitor Platform-level metrics and logs Execution count, CPU/Memory usage, Response times, Custom metrics
Log Streaming Real-time log output Instantaneous view of function execution logs