Overview Of Azure Functions

Introduction

 
Azure Functions is the combination of events and code. This is a completely managed platform with high security and reliability. It is a simplified programming tool which is highly focused on the business model. It is built on top of Azure App Services.

What we will learn here:

  1. Azure Functions and Serverless Computing 
  2. Service plan of Azure Function
  3. Benefits of Azure Functions
  4. Creating an Azure Functions

Azure Functions and Serverless Computing

 
Azure Functions is a serverless architecture. Does that mean there is no server and the code is running without a server? The answer is No! Of course, there is a server where the code will be running, but the key idea is that you can delegate and manage the service through a third party. So, being a developer, we can completely concentrate on the code, not on the server. 
 
Serverless Computing is also known as a "Function-as-a-Service". It eliminates the developer's time to take care of the infrastructure. With serverless, we can simply create and upload the code and then we can define the triggers or events which will execute the function.
 

Service Plan


Dedicated plans – predictable cost; where you can predefine the capacity and scale of the server.

Consumption plan – pay as you go; this plan comes with a monthly free grant of 1 million requests and 400,000 GB of resource consumption per month per subscription in pay-as-you-go pricing across all function apps in that subscription.

Benefits of Azure Functions

  1. Simplified development
  2. All the power of Azure Web Apps is available in Azure Functions
  3. No server is there to maintain
  4. It is used to break the monolithic architecture into loosely-coupled functions
  5. Independent scaling

Creating an Azure Function

We are going to develop an Azure Function using simple webhook template, then code and test it on the Azure portal.

Step 1

Log into portal.azure.com and select Function Apps from Azure Services marketplace.
Step 2
 
You will get a form to create an Azure Function, as shown in the below figure.
 
 
  1. Name the Azure Function; this should be unique.
  2. I'm going to create a new resource group for my Function app.
  3. I'm selecting Windows OS with Runtime stack as .NET since I am going to use C# for programming 
  4. We have two options for hosting plan - Consumption Plan and App Service Plan; choose the desired one.

    Consumption plan is based on the function execution and dynamically allocates the resources based on app load, where you can’t predict the exact cost. App Service plan enables the user to predefine the capacity with predictable cost and scale.

  5. I am going to create a new storage account for my Function app.

Once all fields are completed, click "Create". It will create a Function app.

Step 3 

Create a new function by clicking on "+ New Function" button, as shown in below figure.

Step 4

Choose the development environment; here, I have chosen the Azure Portal.
 
 

Step 5

Choose a "Webhook + API" template which is used to create an API to process the HTTP request.
 
Step 6
 
Initially, you will end up with an editor which preloads the run.csx file, where you can find a predefined method to be fired when the API is called through the endpoint.
 
Basically, this predefined method will process the request to process the request body and concatenate it with the string “Hello”. If there is no request body, it will return as a bad request.
 
The "Run" button is used to build and execute the code; "Save" button is used to save the changes done in the code editor.
 
 
Code Editor in Portal 

Step 7

Click on "</> Get function URL" and you will get an API endpoint.
 
 
 
 

https://myfunctionappdemos.azurewebsites.net/api/HttpTrigger1?code=oRJcOc6j37H9vJKcdZzBRX95wKRsRIhdQaiPiKnNvVvUdgT9i0TYgw==

From the URL, myfunctionappdemos is the Function app name, HttpTrigger1 is the function name, and the query string code is the code to authenticate the API.

Step 8

In the bottom, you can find the logs and console screen. We can test the app using the test functionality which is available in the right corner of the screen, as shown in below image. Click on the "Test" tab and click on the "Run button". It will execute the function and provide the result. The "View files" tab is used to view, add, and modify the files for the function.
 
 
File view 
 
 
Test module in the portal 

Let us test the Azure Function app in Postman.

 
 
Yes, we got a response based on our request. Now, modify the string that is going to return from the Run method in the run.csx file.
 
Updated code  
  1. #r "Newtonsoft.Json"  
  2.   
  3. using System.Net;  
  4. using Microsoft.AspNetCore.Mvc;  
  5. using Microsoft.Extensions.Primitives;  
  6. using Newtonsoft.Json;  
  7.   
  8. public static async Task<IActionResult> Run(HttpRequest req, ILogger log)  
  9. {  
  10.     log.LogInformation("C# HTTP trigger function processed a request.");  
  11.   
  12.     string name = req.Query["name"];  
  13.   
  14.     string requestBody = await new StreamReader(req.Body).ReadToEndAsync();  
  15.     dynamic data = JsonConvert.DeserializeObject(requestBody);  
  16.     name = name ?? data?.name;  
  17.   
  18.     return name != null  
  19.         ? (ActionResult)new OkObjectResult($"Hello, {name} Welcome!")  
  20.         : new BadRequestObjectResult("Please pass a name on the query string or in the request body");  
  21. }  
HTTP response in Postman.
 
 
Yes! We got a response based on our update in Azure Function.
 

Conclusion

 
Thus, we saw what Azure Functions are, and what are the benefits of it. Then, we created our first Azure Function on the Azure portal. We will see more about Azure Functions in my future article.
 
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcomed.