Azure Functions is a serverless cloud service that allows you to write less code, manage less infrastructure, and save money. Rather than worrying about deploying and maintaining servers, the cloud infrastructure provides all the current resources necessary to keep your applications running. You concentrate on the code that matters most to you, while Azure Functions takes care of the rest.
When prompted by events from other services, Azure Functions can be called. The application platform can implement code triggered by events in any third-party service or on-premises system because it is event-driven. Kindly refer to these related guides: How to deploy your Angular App to Azure from Visual Studio Code, how to Setup SonarLint in VS Code for your App Project, how to configure Command Line Interface [Part 2], and How to Serve Private S3 Bucket Contents Via CloudFront.
What is a Function App?
A function app allows you to bundle functions together to make resource management, deployment, scaling, and sharing easier.
Use Case Scenarios of Azure Functions
- Firstly, Azure Functions enables you to turn your system’s logic into easily accessible code chunks. “Functions” are the name for these code blocks. Various functions can be activated at any time when you need to respond to a vital incident.
- Secondly, as demand grows, Azure Functions scales up to meet the demand with as many resources and function instances as are required – but only for as long as they’re needed. Any surplus resources and application instances are immediately decommissioned as demand decreases. What is the source of all computational resources? Azure Functions can supply as many or as few computer resources as you need to meet the demands of your application.
Terminologies that should not be confused with Function Apps
- Logic App: logic apps should not be confused with function apps they are two different things entirely. Azure Logic Apps is a cloud-based solution that lets you create and run automated workflows that connect your apps, data, services, and systems.
- Web job is a code snippet that runs in Azure App Services. It’s a Cloud Service that’s utilized to carry out background tasks. Azure Functions are built on top of Azure Web Jobs and provide additional functionality.
Demo: Create a Function locally and then publish it to an Azure function app in Azure
This demo will be in two parts, we’ll build a basic C# function to handle HTTP requests. In the second part, we’ll deploy it to Azure after building and testing the code locally in Visual Studio Code.
- Part 1.: Create a function app in Azure called “calculator-func-app”
- Part 2: Create a calculator app in Visual Studio that can calculate the sum of x & y and publish it to Azure
Part1: Create an app in Azure called “calculator-func-app”
Log in to the Azure platform, click on the Create a resource button, look for function apps, and input all the required information as shown below:
Operating System: There are two major operating systems available to us as we can see below
Plan/Pricing Tier: It is worth mentioning that there are three types of plans. The default which is
App Service plan. The consumption plan is the default plan, when you’re using the Consumption plan, instances of the Azure Functions host are dynamically added and removed based on the number of incoming events. The Consumption plan is the fully serverless hosting option for Azure Functions.
We should note that the app function has been created in Azure. And it is empty unless we publish code to it from our local development environment.
Part 2: Create a calculator app in Visual Studio that can calculate the sum of x & y and publish it to Azure
The first thing to do if you have not downloaded visual studio is to download it from the link below. You can download the 2022 version but the community edition. Secondly, make sure that you get Visual Studio download azure development kits alongside.
The second step would be to create a new project, function app from the template
Next, give the project a name as shown below
Next, we would be required to choose our stack and trigger. The trigger is especially important for this exercise because we will use the calculator to be able to execute some functionality over a browser so our trigger would be http as shown below
Project Structure & Observations
- Line 15, function name – defines the name of the function note that the name of the class can be different from that of the azure function
- The following line, sixteen, has the method definition, it gets called by the function run time when a function trigger is executed. The first run time of the function method is of the type HTTP request, it also has an attribute of type HTTP trigger. It is a way of saying the function should be run whenever the HTTP trigger gets executed
- The next argument is the logger. it helps you understand what is happening in your system
We renamed the class to sum
We also set the function attribute to sum
We deleted the content of the run method
We kept the log statement of the first line of implementation & also, the return statement
We decided to customize to suit the workings of a calculator as shown below
int x =int.Parse(req.Query[“x”]);
int y =int.Parse(req.Query[“y”]);
int result = x + y;
return new OkobjectResult(result);
The parseInt method parses a value as a string and returns the first integer
The updated code is shown below:
Below is the log information
Test the function to show that the calculator works
we got the link below from the debugger and just added values x=100 and y=20. You can use different values and let’s see
We can also prove this by showing the output of our log console.
Publish to Azure
Now we have to publish to Azure by selecting the Target as shown below
next, select the specific target
Again, select the specific resource group created in Azure
the Zip deploy file is ready to be pushed to Azure Portal
As you can see below, the Azure Function App is currently being Published!
The Azure Function App has been published successfully
Finally, we can see that the process succeeded
Initially, we took a screenshot of the function in the Azure portal to indicate that it was really empty now let's confirm that the function section has a sum in it.
Next, we will have to Confirm and run the sum from the Azure portal
We can click on
sum to get the function URL so as to use it to execute something
Next, we’ll place the URL in a browser and execute a command on it. In this case, the URL is
We need to add
&x=40 &y=18 . The values of x & y you add are up to you
We can see that we are able to achieve the same results we had with the app in visual studio code in Azure Portal. The total number of times the function ran was 4 times and we can see that on the azure platform itself the number of times we performed a sum operation was just 1. So, the interesting thing is this we pay only when the code runs
The azure function is a serverless cloud service and it can help you scale your workload; its architecture is event-driven and as such it’s only when the code runs that we are charged. We were able to build a function app in Visual studio and we successfully pushed it to Azure Portal and used the same app to perform calculations that we previously calculated using when it was in Visual Studio.