Skip to main content
Serverless Architecture
CHAPTER 05 Intermediate

Azure Functions Basics

Updated: May 15, 2026
20 min read

# CHAPTER 5

Azure Functions Basics

1. Introduction

Microsoft Azure commands a massive share of the enterprise market. If your company relies on C#, .NET, Windows, or Office 365, Azure Functions is the natural serverless choice. While AWS Lambda and Google Cloud Functions rely heavily on manual code to connect services together, Azure Functions introduces a brilliant architectural concept called Bindings, which allows your code to read and write to databases without writing any connection logic. In this chapter, we will explore the Azure serverless ecosystem, deploy a Timer function, and master Triggers and Bindings.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the architecture of Azure Functions.
  • Differentiate between Triggers, Input Bindings, and Output Bindings.
  • Understand the Consumption Billing Plan.
  • Deploy a Timer-Triggered Azure Function.
  • Conceptualize an integration using Output Bindings.

3. Beginner-Friendly Explanation

Imagine a post office.
  • The Trigger (The Mail Arriving): A mail truck arrives at the loading dock. This "triggers" the postal worker to start working.
  • The Input Binding (The Mail Sorting Machine): Instead of the worker walking to the truck, digging through bags, and finding the right letters, a machine automatically grabs the exact letters the worker needs and places them directly on their desk before they even start.
  • The Output Binding (The Delivery Chute): When the worker finishes stamping a letter, they don't have to walk outside and put it in a mailbox. They just drop it down a chute next to their desk, and the building automatically delivers it to the correct house.

In Azure, Bindings write all the boring database connection code for you.

4. Triggers vs. Bindings

Every Azure Function must have exactly one Trigger, but it can have zero or multiple Bindings.
  • Trigger: Causes the function to run (e.g., HTTP Request, Timer, Blob Upload).
  • Input Binding: Declaratively connects to a data source to pull data *into* the function before it starts (e.g., automatically fetching a specific row from Cosmos DB).
  • Output Binding: Declaratively pushes data *out* of the function when it finishes (e.g., automatically saving a JSON object to Cosmos DB or sending an SMS via Twilio).

5. Hosting Plans: Consumption vs. Premium

When creating a Function App in Azure, you must choose a hosting plan:
  1. 1. Consumption Plan: True serverless. You pay only for the time your code runs. Scales to zero. Suffers from Cold Starts.
  1. 2. Premium Plan: You pay a flat monthly fee to keep a pool of instances "Pre-warmed." Zero Cold Starts. Excellent for enterprise APIs where a 2-second delay is unacceptable.
  1. 3. App Service Plan: You run the functions on a dedicated Virtual Machine you already pay for. Does not scale to zero.

6. Mini Project: Build an Azure Timer Function

Let's build a serverless "Cron Job" that runs automatically on a schedule.

Step-by-Step Tutorial: *(Assumption: You have an Azure account).*

  1. 1. In the Azure Portal, search for Function App.
  1. 2. Click + Create.
  1. 3. Function App name: my-azure-cron-1234.
  1. 4. Runtime stack: Node.js.
  1. 5. Operating System: Linux.
  1. 6. Plan type: Consumption (Serverless). Click Review + create, then Create.
  1. 7. Once deployed, go to the resource. In the left menu, click Functions, then + Create.
  1. 8. Search for and select the Timer trigger template.
  1. 9. Schedule: Azure uses Cron expressions. Enter 0 * * * * * (This means: Run at second 0 of every minute. Essentially, run once every 60 seconds). Click Create.
  1. 10. Click Code + Test. Look at the code:

javascript
1234
module.exports = async function (context, myTimer) {
    var timeStamp = new Date().toISOString();
    context.log('Azure Timer trigger executed at:', timeStamp);   
};
  1. 11. Click the Logs tab at the bottom of the screen. Wait 60 seconds.
  1. 12. You will see the log output appear automatically! You have built an automated script that runs forever without managing a single server.

7. Real-World Scenarios

A logistics company needs to process shipping manifests. Users upload CSV files to Azure Blob Storage. In a traditional setup, the developer must write 50 lines of C# code to authenticate to the storage account, connect to an Azure SQL Database, and write an SQL INSERT statement. Using Azure Functions, the developer uses a Blob Trigger to start the code, and an Azure SQL Output Binding. The code is reduced to 3 lines: Parse the CSV, and return the object. The Output Binding securely and invisibly inserts the data into the database.

8. Best Practices

  • Use context.log: In Azure Node.js functions, do not use the standard console.log(). You must use context.log() to ensure your logs are properly forwarded to Azure Application Insights for centralized tracking and alerting.

9. Cost Optimization Tips

  • Application Insights Sampling: Azure Functions heavily integrate with Application Insights for monitoring. If your function runs millions of times a day, you will be billed for the massive amount of log data generated. Enable "Sampling" in Application Insights to only record logs for 5% of requests, drastically reducing your observability costs.

10. Configuration Examples

Azure Bindings are defined in a simple function.json file. Here is an example defining an HTTP Trigger and a Cosmos DB Output Binding:
json
123456789101112131415161718
{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["post"]
    },
    {
      "type": "cosmosDB",
      "direction": "out",
      "name": "outputDocument",
      "databaseName": "MyDatabase",
      "collectionName": "MyCollection",
      "connectionStringSetting": "CosmosDbConnection"
    }
  ]
}

11. Exercises

  1. 1. Explain the architectural difference between a Trigger and a Binding in Azure Functions.
  1. 2. In what scenario would an enterprise choose the Premium Plan over the Consumption Plan for an Azure Function?

12. FAQs

Q: Do I write the code in the Azure Portal? A: You can, but professionals don't. Real development is done locally in Visual Studio Code using the "Azure Functions Core Tools" extension, which allows you to run, test, and debug bindings entirely on your local laptop before pushing to the cloud.

13. Interview Questions

  • Q: Describe how Azure Functions Output Bindings simplify interactions with downstream services (like Cosmos DB) compared to traditional SDK implementation. How does this impact security and credential management?
  • Q: Explain the structure of a Cron expression used within an Azure Timer Trigger. How would you configure a function to execute precisely at 2:00 AM every Sunday?

14. Summary

In Chapter 5, we completed our tour of the "Big Three" by exploring Azure Functions. We identified Azure's unique architectural advantage: Bindings, which declaratively abstract the tedious boilerplate code required to connect to databases and storage. We distinguished between the Consumption and Premium hosting plans, and successfully deployed an autonomous Timer Trigger function that executes on a strict schedule without requiring underlying server maintenance.

15. Next Chapter Recommendation

We know how to build Cloud Functions. But exposing raw function URLs directly to the internet or mobile apps is a massive security risk. We need a secure front door. Proceed to Chapter 6: API Gateway and Serverless APIs.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·