ComputeLesson 4 of 16

Azure Functions

Azure Functions is a serverless compute service that runs your code on-demand, billing only for execution time — no servers to manage, no idle costs.

Simple Explanation

A light-switch: you wire it up, and it turns on only when something flips it. Functions execute in response to events, then stop — you pay only for the milliseconds they run.

When to Use Functions

Triggers

TriggerWhen it firesCommon usage
HTTPIncoming web requestLightweight APIs, webhooks
TimerCRON scheduleNightly jobs, cleanup
Blob StorageFile created/modifiedImage resizing, ETL
Queue StorageNew queue messageBackground processing
Service BusMessage or topicEnterprise messaging
Event HubStreaming eventIoT, telemetry
Cosmos DBDocument change feedReplication, sync

Hosting Plans

PlanBilled OnCold Start?Max Timeout
ConsumptionExecutions + GB-sYes (~1-3s)10 min
PremiumPre-warmed instancesNoUnlimited
App ServicePlan tierNoUnlimited
Cold Start Problem

On Consumption plan, if a function hasn't run recently, Azure must provision a new container — adding 1-3 seconds latency. Use Premium plan for user-facing APIs that can't tolerate cold starts.

Visual Representation

Function Execution Flow
Trigger
HTTP / Timer / Blob / Queue
Runtime
Spin up container
Load function code
Inject bindings
Execute
Run your code
Use input bindings
Write output bindings
Bill
~0.000016 $/GB-s
1M free executions/mo

Commands

Azure CLI + Core Tools
# Install Functions Core Tools (local dev)
npm install -g azure-functions-core-tools@4

# Initialize a new function project
func init my-func-app --worker-runtime node

# Create a new HTTP-triggered function
cd my-func-app
func new --name HelloWorld --template "HTTP trigger"

# Run locally
func start

# Create storage account (Functions require one)
az storage account create \
  --name stfuncstorage2024 \
  --resource-group rg-functions \
  --sku Standard_LRS

# Create a Function App (Consumption plan)
az functionapp create \
  --resource-group rg-functions \
  --consumption-plan-location eastus \
  --runtime node \
  --runtime-version 18 \
  --functions-version 4 \
  --name fn-app-myproject \
  --storage-account stfuncstorage2024

# Deploy code
func azure functionapp publish fn-app-myproject

# View logs
az webapp log tail --name fn-app-myproject --resource-group rg-functions

Hands-on

  1. Install Core Tools and create a local HTTP function in Node.js or Python.
  2. Run locally with func start and test via curl http://localhost:7071/api/HelloWorld.
  3. Create a Function App on Consumption plan and deploy.
  4. Add a Timer trigger function with CRON expression 0 */5 * * * * (every 5 min).
  5. View execution logs in the Portal under Monitor.

Debugging Scenario

Issue: HTTP function returns errors only under load.

Interview Questions

Beginner

What is serverless computing?

A model where the cloud provider manages all server infrastructure. You deploy code, pay per execution, and the platform scales automatically — no idle compute costs.

What is a Function trigger?

The event that causes a function to run — HTTP request, timer schedule, queue message, blob upload, etc. Each function has exactly one trigger.

Intermediate

When choose Premium plan over Consumption?

When you need no cold starts (user-facing APIs), longer timeout (> 10 min), VNet integration, or higher performance with predictable costs.

Scenario-based

You need to resize images whenever they are uploaded to blob storage.

Use a Blob Storage triggered Function. The trigger fires on new blob creation, the function reads the blob as input binding, processes it, and writes the resized version as output binding.

Summary

Azure Functions is ideal for event-driven, short-lived workloads. Consumption plan is cheapest for infrequent or variable workloads; Premium removes cold starts for production APIs. Always pair Functions with Application Insights for observability.