Azure Functions - Serverless Computing

What is Azure Functions?

Azure Functions is a **serverless computing service** that lets developers execute event-driven code without managing infrastructure. It allows you to focus on writing logic while Azure handles scaling, maintenance, and execution.

Why Use Azure Functions?

  • Automatic Scaling: No need to manage servers.
  • Event-Driven: Executes only when triggered (e.g., HTTP requests, timers, message queues).
  • Cost-Efficient: Pay only for execution time.
  • Supports Multiple Languages: C#, JavaScript, Python, PowerShell, Java, and TypeScript.
  • Built-in Integration: Works with Azure Storage, Event Grid, Cosmos DB, etc.

Azure Function Triggers

Functions are triggered by specific events. Common triggers include:

  • HTTP Trigger: Executes when an HTTP request is received.
  • Timer Trigger: Runs at scheduled times.
  • Queue Trigger: Processes messages from an Azure Storage Queue.
  • Blob Trigger: Runs when a file is uploaded to Azure Blob Storage.
  • Event Hub Trigger: Responds to events from Azure Event Hub.

Creating an Azure Function (Step-by-Step)

Step 1: Install Azure Functions Core Tools

npm install -g azure-functions-core-tools@4 --unsafe-perm true

Step 2: Create a New Function App

func init MyFunctionApp --worker-runtime node

Step 3: Create a New Function

func new --name HelloWorldFunction --template "HTTP trigger" --authlevel "anonymous"

Step 4: Run the Function Locally

func start

Step 5: Deploy the Function to Azure

az functionapp create --resource-group MyResourceGroup --consumption-plan-location eastus --runtime node --functions-version 4 --name MyFunctionApp

Example: HTTP Trigger Function (JavaScript)

This function responds to HTTP requests and returns "Hello, Azure Functions!".

module.exports = async function (context, req) {
    context.res = {
        status: 200,
        body: "Hello, Azure Functions!"
    };
};

Scaling Azure Functions

Azure Functions automatically scale based on execution needs. You can also manually set limits:

az functionapp scale --resource-group MyResourceGroup --name MyFunctionApp --max-worker-count 10

Monitoring and Debugging

Monitor function execution using Azure Monitor and Application Insights.

az monitor log-analytics workspace create --resource-group MyResourceGroup --workspace-name MyWorkspace

Cost Optimization Tips

  • Use the **Consumption Plan** for low-cost execution.
  • Disable functions when not in use.
  • Optimize function execution time to reduce costs.

Conclusion

Azure Functions provide a **powerful, scalable, and cost-efficient** way to run event-driven code in the cloud. Whether you're handling HTTP requests, automating tasks, or integrating with Azure services, Functions make development easier.

📌 Next Topic: Azure Blob Storage - Managing Cloud Files