Saturday, September 12, 2020

Azure Function or Serverless Function

In this blog I covered what is Azure function? Why to use azure function? How to create azure function from azure portal and how to pass parameters in the function (by bindings)? Different way to invoke the function?

1. What is Azure Function or ServerLess Function?

Name serverless function is very confusing, this does not mean that without server azure function or method will exist and execute 😉. Azure Function code exist in the azure cloud environment. Suppose you want to write a method which can execute in the remote machine or you want to execute this method from anywhere of the world or event base you want to trigger the method however you don't want to buy any hardware, not to worry on load balancing, networking and security then you can think for azure function as a solution.

This is FAAS (functions-as-a-service), azure give this service on the method basis. Azure function is auto scalable and can achieve HA (high availability).

2. Why to use Azure Function:

  • Suppose you have very critical piece of code which you want to be up and ruining with 99.99 % or HA but you don't have budget for load balancing, then you can think for azure function
  • On schedule time you want to execute some piece of code
  • Develop mico-services
  • Processing bulk data

3. Different ways to invoke Azure Function:

  • HTTP Request
  • Schedule Timer
  • When new or modified Azure Cosmos DB documents
  • When new or modified Azure Storage blobs
  • Azure Storage queue messages
  • Azure Event Grid events via subscriptions and filters
  • High-volumes of Azure Event Hub events

4. Bindings in Azure Function:

Bindings is the data connection to the function, in other word function need some data to compute and give the result , so azure function provide binding direction to in and out the data from function. 

There are there type of binding directions:

  • In: Only passing the parameter value
  • In and Out: Pass value as parameter and return some value
  • InOut

5. Create Http Azure Function:

Create an http function by passing two numbers and display sum of these two numbers. I am taking this example to understand binding direction also.

Step 1. Login to the portal.azure.com and click on Azure Function App.

Step 2. Click on the "+ Add" icon.
Step 3.Create a resource group or select the existing one if you already created resource group, fill the "function app name", select runtime stack (with this you can select the language to write the code) and select the region:
click on "Review + Create" button. 

Step 4. Review complete details and click on "Create". This will take some time as "Deployment is in progress"..
Step 5. After deployed click on "Go To Resource"
Step 6. Click on "Function"
Step 7. Click on "+ Add" icon
Step 8. Click on "HTTP trigger" icon
Step 9. Enter the "function name" and "authorization level" let it be as "Function" and click on "Create Function"..
Step 10. Click on "Code + Test" to view and modify the code.
Step 11. Modify the function code as below and copy the function URL from "get function URL".

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    int num1 = Convert.ToInt32(req.Query["num1"]);
    int num2 = Convert.ToInt32(req.Query["num2"]);

    string responseMessage = "Sum of two numbers is: " + Convert.ToString(num1 + num2);

            return new OkObjectResult(responseMessage);
}
Step 12
. Append the URL with "&num1=10&num2=20" e.g. https://dot-net-tutorials-function.azurewebsites.net/api/Add?code=gHFqkanPjrcarP6Hcfl48W9bEnueGXMr9mC/gjavjWcyVbacmz0CEA==&num1=10&num2=20 

Step 13. Paste the URL to browser to test the function and check the result.

In the above example code "in" binding been passed as query string, num1=10 and num2=20 to the azure function and out result(add) shown as 30

4 comments:

  1. It would be great if you could elaborate more on authorization levels... how it is useful in the context of azure functions...

    ReplyDelete
  2. What are the different security mechanism it provides?
    When I can consume this function in my application, what is the type of authentication or security to access is provided?

    ReplyDelete
  3. If I want to use 3rd party library in these functions, how I can use?

    ReplyDelete
  4. How many TPS support it has? Can I scale this?
    If we have a requirement of 100 calls per second... how we can scale and support this?

    ReplyDelete

Azure Function or Serverless Function

In this blog I covered what is Azure function? Why to use azure function? How to create azure function from azure portal and how to pass pa...