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. Consumption Plan: True serverless. You pay only for the time your code runs. Scales to zero. Suffers from Cold Starts.
- 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.
- 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. In the Azure Portal, search for Function App.
- 2. Click + Create.
-
3.
Function App name:
my-azure-cron-1234.
-
4.
Runtime stack:
Node.js.
-
5.
Operating System:
Linux.
-
6.
Plan type:
Consumption (Serverless). Click Review + create, then Create.
- 7. Once deployed, go to the resource. In the left menu, click Functions, then + Create.
- 8. Search for and select the Timer trigger template.
-
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.
- 10. Click Code + Test. Look at the code:
javascript
- 11. Click the Logs tab at the bottom of the screen. Wait 60 seconds.
- 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 SQLINSERT 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 standardconsole.log(). You must usecontext.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 simplefunction.json file. Here is an example defining an HTTP Trigger and a Cosmos DB Output Binding:
json
11. Exercises
- 1. Explain the architectural difference between a Trigger and a Binding in Azure Functions.
- 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?