Azure Functions Tutorial: A Beginner's Guide with Real-World Examples
Azure Functions is a serverless compute service that lets you run small pieces of code, called functions, in response to events — without provisioning or managing servers.
Key Concepts
- Trigger — defines what causes a function to run, such as an HTTP request, a timer, or a new message in a queue
- Binding — a declarative way to connect a function to other services (input or output) without writing extra integration code
- Function App — the container that hosts one or more individual functions
Example: HTTP-Triggered Function (Node.js)
module.exports = async function (context, req) {
const name = req.query.name || "World";
context.res = {
body: `Hello, ${name}!`
};
};
Example: Timer-Triggered Function
module.exports = async function (context, myTimer) {
context.log("Timer function executed at", new Date().toISOString());
};
Real-World Use Cases
- Processing uploaded files automatically when they land in Blob Storage
- Sending notifications or emails when a new record is added to a database
- Running scheduled cleanup or reporting jobs on a timer trigger
- Building lightweight backend APIs for mobile or web applications
Why Use Azure Functions?
- Pay only for the compute time your function actually uses (Consumption plan)
- Automatically scales based on the volume of incoming events
- Reduces operational overhead since there's no server to patch or manage
Ready to master Microsoft Azure?
Join Microsoft Azure Training Course at Uncodemy and learn with hands-on projects and mentor support.
Creating and Running a Function Locally
You do not need the Azure portal to start. Azure Functions Core Tools lets you create, run and debug functions on your own machine, then publish them when ready. With Node.js and Core Tools installed:
# create a new function app project
func init OrdersApp --javascript
cd OrdersApp
# add an HTTP-triggered function from a template
func new --name hello --template "HTTP trigger"
# run everything locally (prints a local URL to call)
func start
# publish to an existing Function App in Azure
func azure functionapp publish my-orders-func
Local settings such as connection strings go in local.settings.json, which should never be committed to source control. In Azure, the same values are set as application settings on the Function App.
The Newer Node.js Programming Model
The examples above use module.exports with a context object, which is the older style. New Node.js projects use a model where triggers are registered in code through the app object, so a separate function.json file is not needed.
const { app } = require('@azure/functions');
app.http('hello', {
methods: ['GET'],
authLevel: 'anonymous',
handler: async (request, context) => {
const name = request.query.get('name') || 'World';
return { body: `Hello, ${name}!` };
}
});
app.storageBlob('processUpload', {
path: 'uploads/{name}',
connection: 'AzureWebJobsStorage',
handler: async (blob, context) => {
context.log(`Received ${context.triggerMetadata.name}, ${blob.length} bytes`);
}
});
The second function runs automatically whenever a file lands in the uploads container, which is the image-processing or document-parsing pattern many teams start with.
Hosting Plans in Plain Terms
- Consumption and Flex Consumption: scale with demand and can scale down to nothing when idle, billed for what runs. Expect occasional cold starts after idle periods.
- Premium: keeps instances warm to reduce cold starts, adds virtual network integration and suits steady or latency-sensitive workloads.
- Dedicated (App Service plan): runs on servers you already pay for, useful when you have spare App Service capacity.
Check the current Microsoft documentation for each plan's timeout and scaling behaviour before designing around them, as these details differ by plan.
When You Need State: Durable Functions
Plain functions should be stateless. If a workflow has several steps, waits for approval, or fans out to many parallel tasks and then combines the results, use the Durable Functions extension. An orchestrator function describes the workflow in code and the framework stores progress, so the process survives restarts.
Common Beginner Mistakes
- Creating a new HTTP or database client inside every invocation. Create it once outside the handler so connections are reused.
- Assuming a function runs exactly once. Triggers like queues can retry, so make handlers safe to run twice.
- Leaving HTTP functions anonymous in production without any authentication in front.
- Writing long-running loops in one function instead of splitting work through queues or Durable Functions.
- Ignoring Application Insights, which is where logs and failures are easiest to trace.