Introduction
These days, Serverless computing is one of the key terms in the Cloud. This is a PaaS (Platform as a Service). It allows us to deploy and manage individual code functions without a need to deploy and manage on the individual Virtual Machine (VM). Serverless architecture does not mean that we host applications without servers but Serverless really talks about the PaaS.
Azure Functions is a serverless compute service that runs our code on-demand without needing to host it on the server and managing infrastructure. Azure Functions can be trigger by a variety of events. It allows us to write code in various languages, such as C#, F#, Node.js, Java, or PHP.
Azure Functions provide the following features:
Flexible development
We can write our code directly to the portal or can set up continuous integration and deploy our code through Azure DevOps, GitHub, or any other supported development tools.
Integrated security
We can protect HTTP-triggered function with OAuth providers, such as Azure AD, Microsoft account, or other third-party accounts, such as Google, Facebook, and Twitter.
Allow selecting development language
It allows us to write a function in the language of our choice, such as C#, F#, Java, PHP, JavaScript, etc. You can check the list of supported languages
here.
Bring our own dependencies
Azure Functions allow us to download code dependencies from NuGet and NPM, so we can use libraries that need to execute our code and are available on either NuGet or NPM.
Pay per use price model
Azure provides a flexible consumption plan as per our requirement. Also, if we already use App service for our other application, we can run the function on the same plan. So, there is no additional cost we need to pay.
Open Source
The function runtime is open source.
Azure Function Trigger
The Azure function is the best solution for working with IoT devices, building small API and microservice, any data processing. Azure Functions support triggers that are the way to start the execution of our code. Azure Function provides the following templates to get it triggered
- HTTPTrigger
- TimerTrigger
- CosmosDBTrigger
- BlobTrigger
- QueueTrigger
- EventGridTrigger
- EventHubTrigger
- ServiceBusQueueTrigger
- ServiceBusTopicTrigger
In this article, I will explain how to create an Azure Function with HTTP trigger using
Azure portal. The same function can be developed using an editor, such as Visual Studio or VS Code and publish to Azure portal. To do the hands-on, you need an Azure subscription.
Following are the steps to create Azure Function on Azure Portal
Step 1
Click on "Create a resource" link and then select "Compute" option from the marketplace and click on "Function App" from the Featured tab.
Step 2
Provide App name, subscription, resource group, OS, hosting plan, location, and storage and then click on the "Create" button.
Step 3
Wait. The Azure Portal will create a Function app and host/deploy it. Once it deploys, a notification will be displayed. We can jump to the "Create function" screen by clicking "Get to resource" button.
Step 4
Click on the (+) or the “New function" button to create a function under Function App.
Step 5
In this step, we need to select the development environment. To demonstrate the concept, I have selected "In-portal" option and clicked on the "Continue" button.
Step 6
In this step, we need to select the template that is used to create the function. Here, I have selected "Webhook + API" and then, clicked on the "Create" button.
Step 7
It will create a function with the demo code. Here, we can write our own code that needs to be executed when any HTTP call happens.
Here, I have used "HttpTrigger" template. So, when we make an HTTP call, our function gets executed. We can select the HTTP method on which the function gets executed.
In this demo, I am using the following code. This code only executes when we make a POST request and it will expect FirstName and LastName in the request body. If the request body does not contain these fields, it will be treated as a bad request.
- #r "Newtonsoft.Json"
-
- using System.Net;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Primitives;
- using Newtonsoft.Json;
-
- public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
- {
- string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
- dynamic data = JsonConvert.DeserializeObject(requestBody);
- string name = data?.FirstName + " " + data?.LastName;
-
- return (name != null && !string.IsNullOrWhiteSpace(name))
- ? (ActionResult)new OkObjectResult($"Hello, {name}")
- : new BadRequestObjectResult("Please pass a name in the request body");
- }
You can get the request URL by clicking on the "Get Function URL" link. Just copy this link and use it for testing your Azure function.
I am using Postman to test my Azure function. Here, I have used content-type as application/json and the request body contains the JSON data as shown in the following image. Here, I have made the POST request. As we have shown here, we will get the status code 200 in response and the response body contains the text "Hello {name}".
If I haven't passed the request body when we make a request, it will return the 400 status code.
Limitations
- Troubleshooting is very difficult in production
- The latency of response- There is some initial booting time of Azure function and may have latency over the network.
- Serverless computing relies on third-party services so, we need to trust the response and security of these services and, third-party vendor any time stop the services
- It is very difficult to manage when too many functions in an application. It may result in very complex architecture.
Summary
Serverless computing is a model where the developers build an application but do not worry about the infrastructure to host the application.
It is like PaaS but the pay model is different.
Azure Functions is a serverless compute service which runs our code on-demand without needing to host that on the server and managing infrastructure. Azure Functions are also known as "functions" in the cloud. It also allows us to write our code in various languages, such as C#, F#, PHP, Java, etc.