Previous Azure-API-App Azure-Function-Deployment-Steps Next

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.ScheduleStatus to inspect next run time
  • Ensure AzureWebJobsStorage is configured in local.settings.json
  • Use UseDevelopmentStorage=true for 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.

πŸ§ͺ 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.

Back to Index
Previous Azure-API-App Azure-Function-Deployment-Steps Next
*