| Azure-API-App :👈 | 👉:Azure-Function-Deployment-Steps |
Azure Functions |
π What Are Azure Functions?
Azure Functions is a serverless compute service offered by Microsoft Azure that lets you run small pieces of code β called functions β without worrying about infrastructure. You write the logic, Azure handles the rest: provisioning, scaling, and managing servers. It automatically scales based on demand and you only pay for the execution time your code consumes.
βοΈ What Makes Azure Functions Powerful
- Event-driven: Triggered by HTTP requests, timers, queues, blob storage changes, etc.
- Scalable: Automatically scales based on demand.
- Cost-efficient: You only pay for the time your code runs.
- Multi-language: Supports C#, JavaScript, Python, Java, PowerShell, and more.
- Integrated: Works seamlessly with other Azure services like Cosmos DB, Event Grid, and Logic Apps.
β‘ Azure Functions: Types & Timer Scheduling
π¦ Types of Azure Function Triggers
- HTTP Trigger: Executes when an HTTP request is received
- Timer Trigger: Executes on a defined schedule (CRON expression)
- Blob Trigger: Executes when a blob is added or modified in Azure Storage
- Queue Trigger: Executes when a message is added to an Azure Storage Queue
- Event Grid Trigger: Executes when an event is published to Event Grid
- Event Hub Trigger: Executes when events are received in Event Hub
- Cosmos DB Trigger: Executes on changes in a Cosmos DB collection
β° How to Schedule a Timer Trigger Function
A Timer Trigger runs based on a CRON expression. Here's how to define it in C#:
Example: Run Every 5 Minutes
[Function("ScheduledTask")]
public void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
log.LogInformation($"Timer triggered at: {DateTime.Now}");
}
CRON Expression Format
{second} {minute} {hour} {day} {month} {day-of-week}
"0 */5 * * * *"β Every 5 minutes"0 0 9 * * *"β Every day at 9 AM"0 0 * * 1"β Every Monday at midnight
π Setup Tips
- Use
TimerInfo.ScheduleStatusto inspect next run time - Ensure
AzureWebJobsStorageis configured inlocal.settings.json - Use
UseDevelopmentStorage=truefor local testing
β Summary
Azure Functions support various triggers for event-driven execution. Timer Trigger is ideal for scheduled tasks like cleanup jobs, data sync, or reporting. Use CRON expressions to control when your function runs.
The default timeout for Azure Functions depends on the hosting plan you're using.
| Hosting Plan | Default Timeout | Maximum Timeout |
|---|---|---|
| Consumption (legacy) | 5 minutes | 10 minutes (configurable) |
| Premium | 30 minutes | Effectively unlimited for function execution (configured through functionTimeout) |
| Dedicated (App Service Plan) | 30 minutes (Functions v2+) | Unlimited when configured appropriately |
| Flex Consumption | Supports much longer executions than the legacy Consumption plan and is the recommended serverless option for new apps. Microsoft documents timeout behavior in the hosting plan guidance. |
Key points
- β The often-quoted Azure Functions timeout of 5 minutes applies to the legacy Consumption plan, not to all hosting plans.
- β Premium and Dedicated/App Service plans are designed for longer-running workloads and can be configured for very long or effectively unlimited execution times.
- β Timeout settings are controlled through the functionTimeout setting in the host.json file
π§ͺ Example: Azure Function in C# (HTTP Trigger)
Hereβs a simple C# function that responds to an HTTP request:
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
public class HelloFunction
{
private readonly ILogger _logger;
public HelloFunction(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger();
}
[Function("HelloFunction")]
public HttpResponseData Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Hello from Azure Function!");
return response;
}
}
π How It Works
- Trigger: HTTP GET or POST request.
- Response: Returns a plain text greeting.
- Logging: Writes to Azure Monitor logs.
π‘ Use Case: This kind of function is perfect for lightweight APIs, webhooks, or automation endpoints.
Key Benefits
- Serverless β no infrastructure to manage
- Automatic scaling
- Supports multiple languages (.NET, JavaScript, Python, Java, PowerShell, etc.)
- Integrates easily with other Azure services
- Pay-per-execution pricing model
Another Example: HTTP Trigger Function in C#
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Get name from the query string
string name = req.RequestUri.ParseQueryString().Get("name");
// Return a response
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string")
: req.CreateResponse(HttpStatusCode.OK, "Hello, " + name);
}
How it Works
When deployed, this Azure Function is triggered by an HTTP request. For example:
GET https://your-function-app.azurewebsites.net/api/hello?name=Alice
Response:
Hello, Alice
Azure Function Example in JavaScript
This example shows an HTTP-triggered Azure Function written in Node.js.
It takes a name query parameter and returns a greeting.
Example Code (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);
if (name) {
context.res = {
status: 200,
body: "Hello, " + name
};
} else {
context.res = {
status: 400,
body: "Please pass a name in the query string or in the request body"
};
}
};
How it Works
When deployed, this Azure Function is triggered by an HTTP request. Example call:
GET https://your-function-app.azurewebsites.net/api/hello?name=Alice
Response:
Hello, Alice
π‘ Pro Tip: Use JavaScript Azure Functions for lightweight APIs, automation scripts, or quick integrations with other services. Azure Functions are ideal for event-driven tasks like data processing, integrating with APIs, or running scheduled jobs.
| Azure-API-App :👈 | 👉:Azure-Function-Deployment-Steps |