AWS/Azure/OpenShift Windows

Serverless in Azure: How to deploy a function app from Visual Studio to Azure Platform

feature-functionapp

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. In this article, we shall discuss “Serverless in Azure: How to deploy a function app from Visual Studio to Azure Platform”. Here are the steps on how to Change Visual Studio Code UI language, and How to make a router function as a switch in GNS3.

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.

Please see how to fix “Unable to change screen brightness and volume etc: Disable standard function keys on Mac“, how to create “Profiles for your AWS Access Credentials for AWS Toolkit in Visual Studio“, and Windows Server: What are the differences between a role and a feature.

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: This 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:

function-app
function app

Operating System: There are two major operating systems available to us as we can see below

operating-system
operating systems

Plan/Pricing Tier

It is worth mentioning that there are three types of plans. The default which is consumption plan, premium and 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.

plan
plan
 You 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.

functions-in-azure
empty function

Part 2: Create a calculator app in Visual Studio that can calculate the sum of x & y and publish it to Azure

Note: 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

vscode
create a new project

Give the project a name as shown below

name
name
Now you are required to choose your 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

trigger
trigger

created

created2
code

Project Structure & Observations

1: 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
2: 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. That is another way of saying the function should be run whenever the HTTP trigger gets executed.
3: 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:

image-65

Below is the log information

sum-log
function is logging 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

http://localhost:7071/api/sum?x=100&y=20
TEST

We can also prove this by showing the output of our log console.

executed-sum
sum executed

Publish to Azure

Now we have to publish to Azure by selecting the Target as shown below

azure
Azure account

next, select the specific target

specific-target
Azure function as target

Again, select the specific resource group created in Azure

resouceg
specific resource group

Now, the Zip deploy file is ready to be pushed to Azure Portal.

zipdeploy-created
ready to go

The image below shows that the Azure Function App is currently being Published!

finally
publishing

Simultaneously, the Azure Function App is successfully published.

succeeded
done

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.

We will have to Confirm and run the sum from the Azure portal

sumazure
function arrived azure

Click on sum to get the function URL so as to use it to execute something

function-url
function url

Next, we’ll place the URL in a browser and execute a command on it. In this case, the URL is

https://calculator-func-app.azurewebsites.net/api/sum?code=kcbIfp7GoRFCZF1HDygDnWI-LuQRGMWieMJ7p6uE96KpAzFucTkPEQ==
We need to add &x=40 &y=18. The values of x & y you add are up to you 
https://calculator-func-app.azurewebsites.net/api/sum?code=kcbIfp7GoRFCZF1HDygDnWI-LuQRGMWieMJ7p6uE96KpAzFucTkPEQ==&x=40 &y=18
result
successful
execution-times
execution times

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

Summary

Microsoft Azure function is a serverless cloud service that helps you scale your workload. Its architecture is event-driven and as such, you are charged only when the code runs.

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.

I hope you found this blog post helpful on “Serverless in Azure: How to deploy a function app from Visual Studio to Azure Platform”. Please let me know in the comment section if you have any questions.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x