1---2name: azure-functions3description: Use when building serverless event-driven applications with Azure Functions, designing Durable Functions orchestration workflows, choosing hosting plans, or studying for Azure AI Cloud Developer Associate (AI-200/AZ-204).4---56# Azure Functions78## When to Use9- Building serverless, event-driven compute on Azure10- Designing Durable Functions for stateful orchestration workflows11- Choosing between Consumption, Premium, and Dedicated hosting plans12- Implementing input/output bindings to reduce boilerplate SDK code13- Connecting Functions to Service Bus, Event Grid, Cosmos DB, or Blob Storage14- Preparing for Azure AI Cloud Developer Associate (AI-200/AZ-204) exam1516## Core Jobs1718### 1. Triggers19| Trigger | Description |20|---------|-------------|21| **HTTP** | REST endpoint; function-level or host-level key auth |22| **Timer** | CRON expression schedule (runs in UTC by default) |23| **Blob Storage** | New/modified blob in container |24| **Queue Storage** | New message in Azure Queue Storage |25| **Service Bus** | Message from Service Bus queue or topic |26| **Event Hubs** | Event batch from Event Hub consumer group |27| **Event Grid** | Event Grid event (cloud events or Event Grid schema) |28| **Cosmos DB** | Change feed trigger on Cosmos DB container |2930- One function = one trigger; multiple triggers require separate functions3132### 2. Hosting Plans33| Plan | Cold Start | Scale | VNet | Best For |34|------|-----------|-------|------|---------|35| **Consumption** | Yes | 0 → 200 instances auto | No | Bursty, intermittent workloads |36| **Flex Consumption** | Minimal | 0 → custom auto | Yes | Serverless + VNet requirement |37| **Premium (EP)** | No (pre-warmed) | Min 1, auto scale | Yes | No cold start + VNet + longer timeout |38| **Dedicated (App Service)** | No | Manual or auto | Yes | Predictable always-on workloads |3940- **Cold start** = first request after scale-to-zero spin-up delay (100ms–2s typically)41- **Premium plan** = pre-warmed instances eliminate cold starts; needed for VNet integration + Consumption42- **Consumption plan timeout** = 5 minutes default, 10 minutes max; Premium = 30 minutes default, unlimited4344### 3. Durable Functions45Stateful function orchestration patterns:46| Pattern | Description |47|---------|-------------|48| **Function chaining** | Sequential calls: F1 → F2 → F3; output of one is input to next |49| **Fan-out/fan-in** | Parallel execution of multiple activity functions; wait for all |50| **Async HTTP API** | Long-running operation; return 202 + status URL immediately |51| **Monitor** | Polling loop that checks external status and waits until complete |52| **Human interaction** | Wait for external event (approval); timeout with escalation |5354- **Orchestrator function** = deterministic; no I/O, random, DateTime.Now directly; use activity functions for these55- **Activity function** = does actual work; called by orchestrator; can retry on failure56- **Entity function** = stateful actors; manage state with operations5758### 4. Input and Output Bindings59- Bindings = declarative connections to Azure services; reduce SDK boilerplate60- Input binding: read data when function executes (e.g., read Blob, read Cosmos document)61- Output binding: write data after function executes (e.g., write to Queue, write to Table)62- Example (C# attribute): `[BlobInput("container/{filename}")] string blobContent`63- Example (Python decorator): `@app.blob_input(arg_name="blob", path="container/{filename}")`64- Supported: Blob, Queue, Table, Service Bus, Event Hub, Cosmos DB, SignalR, SendGrid, Twilio6566### 5. Managed Identity Authentication67- **System-assigned Managed Identity**: identity tied to Function App lifecycle; deleted with app68- **User-assigned Managed Identity**: standalone identity; reusable across multiple resources69- `DefaultAzureCredential` (Azure SDK): tries Managed Identity, Visual Studio, Azure CLI in sequence70- Assign RBAC role to Function's Managed Identity on target resource (e.g., `Storage Blob Data Reader`)71- Never store connection strings or secrets in code; use Key Vault references in App Settings7273### 6. Function Keys and Auth74| Auth Level | Scope | Use Case |75|------------|-------|---------|76| **Anonymous** | No key required | Public endpoints (webhooks with own auth) |77| **Function** | Per-function key | Caller has function-specific key |78| **Host** | All functions in app | Caller has master key for all functions |79| **Admin** | Management operations | Admin key has full host-level access |8081- HTTP trigger default: function-level auth82- For machine-to-machine auth in Azure: prefer Managed Identity over function keys8384## Key Concepts85- **Consumption plan** — true serverless; scales to zero; cold starts; pay-per-execution86- **Premium plan** — pre-warmed instances; no cold start; VNet integration; higher baseline cost87- **Durable orchestrator** — must be deterministic; no direct I/O; calls activity functions for side effects88- **DefaultAzureCredential** — SDK credential chain; uses Managed Identity in production automatically89- **Binding** — declarative trigger/input/output connection; avoids SDK plumbing code90- **Timer trigger CRON** — UTC by default; format: `{second} {minute} {hour} {day} {month} {weekday}`9192## Checklist93- [ ] Hosting plan chosen based on cold start tolerance, VNet need, and workload pattern?94- [ ] Managed Identity used instead of connection strings for Azure service authentication?95- [ ] Durable orchestrator functions kept deterministic (no DateTime.Now, random, direct I/O)?96- [ ] Output bindings used to reduce boilerplate for writing to Queue/Blob/Cosmos?97- [ ] Function timeout configured appropriately for hosting plan (Consumption max = 10 min)?98- [ ] Key Vault references used for secrets in App Settings (not hardcoded values)?99- [ ] Retry policy configured for transient failure scenarios (Service Bus, Cosmos)?100101## Output Format102- 🔴 **Critical** — DateTime.Now or random number generator directly in Durable orchestrator (breaks replay determinism)103- 🔴 **Critical** — connection strings hardcoded in function code or app settings (use Key Vault references)104- 🟡 **Warning** — Consumption plan used when VNet integration is required (use Premium or Flex Consumption)105- 🟡 **Warning** — function timeout exceeded without Premium plan (Consumption max = 10 min)106- 🟢 **Suggestion** — use DefaultAzureCredential with user-assigned Managed Identity for cross-resource auth107108## Exam Tips109- **Consumption plan = true serverless; scale to zero; cold starts** — Premium plan = pre-warmed, no cold start, VNet integration110- **Durable orchestrator = deterministic** — never put I/O, DateTime.Now, or random directly in orchestrator; delegate to activity functions111- **Bindings reduce boilerplate** — output binding writes to Queue/Blob without SDK calls; declared in function.json or decorators112- **Timer trigger = CRON expression in UTC** — format: `0 0 * * * *` = every hour; `0 */5 * * * *` = every 5 minutes113- **Managed Identity = no credentials in code** — assign RBAC role to Function's Managed Identity; use DefaultAzureCredential in SDK114- **Function keys: function vs host vs admin** — function key = per-function; host key = all functions; admin key = management operations (protect carefully)