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:
- Azure Functions and Serverless Computing
- Service plan of Azure Function
- Benefits of Azure Functions
- 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
- Simplified development
- All the power of Azure Web Apps is available in Azure Functions
- No server is there to maintain
- It is used to break the monolithic architecture into loosely-coupled functions
- 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.
- Name the Azure Function; this should be unique.
- I'm going to create a new resource group for my Function app.
- I'm selecting Windows OS with Runtime stack as .NET since I am going to use C# for programming
- 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.
- 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.
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.
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
- #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)
- {
- log.LogInformation("C# HTTP trigger function processed a request.");
-
- string name = req.Query["name"];
-
- string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
- dynamic data = JsonConvert.DeserializeObject(requestBody);
- name = name ?? data?.name;
-
- return name != null
- ? (ActionResult)new OkObjectResult($"Hello, {name} Welcome!")
- : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
- }
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.