Azure Functions is a serverless compute service that lets you run small pieces of code, or "functions," without managing infrastructure. It's event-driven, scalable, and cost-effective. Understanding the core concepts is key to building powerful serverless applications.
A function is the fundamental unit of work in Azure Functions. It's a piece of code that runs in response to an event. Functions can be written in various programming languages, including C#, JavaScript, Python, Java, and PowerShell.
Triggers define how a function is invoked. They are the events that initiate function execution. Azure Functions supports a wide range of triggers, such as:
A function must have exactly one trigger.
Bindings simplify your code by allowing you to connect to other Azure services and SaaS solutions without complex SDKs or explicit integration code. They handle input and output operations. There are two types of bindings:
Bindings are declared in the function's configuration (e.g., function.json for JavaScript/Python or attributes in C#/Java).
A trigger defines how a function is invoked, while bindings provide a declarative way to access other services as input or output to your function.
A Function App is the logical collection of functions that you manage together. It allows you to group related functions, share configuration, and manage deployment. Functions within the same Function App run in the same host process.
Azure Functions offers several hosting plans, each with different pricing, performance, and scaling characteristics:
The Azure Functions runtime is the engine that hosts and executes your functions. It manages triggers, bindings, and the execution environment.
By default, Azure Functions are stateless. Each function execution is independent of others. If you need to maintain state, you can use external services like Azure Storage or Azure Cosmos DB.
Durable Functions is an extension of Azure Functions that allows you to write stateful functions in a serverless compute environment. It enables you to orchestrate and manage complex workflows, including long-running processes, fan-out/fan-in patterns, and human interaction.
Key concepts in Durable Functions include:
Consider a simple HTTP-triggered function that reads a value from a query string, writes it to a queue, and returns a success message.
function.json (for JavaScript)
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "queue",
"direction": "out",
"name": "outputQueueItem",
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage"
}
]
}
index.js (for JavaScript)
module.exports = async function (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. Passing " + name + " to queue."
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
context.bindings.outputQueueItem = name; // Write to the queue
context.res = {
// status: 200, /* Defaults to 200 */
body: responseMessage
};
};
In this example:
httpTrigger binding invokes the function.http binding defines the HTTP response.queue binding (output) sends the value of the name variable to a queue named myqueue-items.