How To Create And Deploy A Serverless Function On Azure With .Net Core

Hey there..

Today we are going to analyze Serverless Functions In Azure. But firstly it s good to learn what is Serverless. Let’s start

What Is Serverless ?

The most popular question about serverless is “Is not there any server behind the system” and “how does codes work without a server”. We should clarification. Of course there are servers behinde the system. The mean of serverless here is when you write the code you don’t need to do anything about server. We will just write our function and deploy to cloud, that’s all ?

-Yes that’s all!

The Serverless word can confuse mind that s why we have another name that we call serverless. Functions as a Service (FaaS).

What Are Advantages Of Serverless ?

  • Cheap
  • You don’t need to scale your code
  • You don’t need to think about server or OS
  • You don’t need to think upgrade your program language

Today we will create a serverless function in Azure Cloud. Azure provide us 1,000,000 requests per month as free service. You can create and free account to try this tutorial below the link.

After we create our account and logged in to our dashboard, we can start our process with click Function App button. And in the new page we click Create Function App

With our sidemenu form we can configure our application details. In this example i preferred windows OS and .Net stack to develop my functions. After we done we click Create button

Our application is ready to create our serverless function. We click the name of our application and then we click New Function.

Azure provide us to develop this function on different platforms. I used VsCode to develop that function and choose Direct publish because we will publish our function from VsCode

Okay, we’ve done our configuration steps for now, right now the things that we need to have :

Install .Net Core

Node.Js to install npm

Visual Studio Code

At the last step we will install extension of Azure to Visual Stuido Code. We open visual studio and then we write that line below from the terminal to install our extension.

npm install -g azure-functions-core-tools

Right now we can create our function. On the side menu you can click Azure Logo. On the functions bar click the icon that shown picture below. Then you will see the function types that you can create. In this example we will select HttpTrigger.

What Are The Other Types?

  • HTTPTrigger : Trigger the execution of your code by using an HTTP request.
  • TimerTrigger : Execute cleanup or other batch tasks on a predefined schedule.
  • CosmosDBTrigger : Process Azure Cosmos DB documents when they are added or updated in collections in a NoSQL database.
  • BlobTrigger : Process Azure Storage blobs when they are added to containers. You might use this function for image resizing.
  • QueueTrigger : Respond to messages as they arrive in an Azure Storage queue.
  • EventGridTrigger : Respond to events delivered to a subscription in Azure Event Grid. Supports a subscription-based model for receiving events, which includes filtering. A good solution for building event-based architectures.
  • EventHubTrigger — Respond to events delivered to an Azure Event Hub. Particularly useful in application instrumentation, user experience or workflow processing, and internet-of-things (IoT) scenarios.
  • ServiceBusQueueTrigger : Connect your code to other Azure services or on-premises services by listening to message queues.
  • ServiceBusTopicTrigger : Connect your code to other Azure services or on-premises services by subscribing to topics.

We are ready to create our function. Our function gonna be quite simple. We will get two numbers from querystring and sum them. Finally we will return the result inside OkObjectResult.

As we develop normal project, we can test and debug our function as well. After write your code you can run your function on localhost with F5. You can see the localhost link in terminal.

As you seen below we sent 2 and 3 as parameters and we get the correct result.

We developed and tested our function. Right now we are ready to deploy it. From the same Functions bar we will click the deploy icon and select our application.

We deployed our function successfully. Now we return to our Azure Function Dashboard and open our functions. As you seen picture below. You can get function url and paste to your browser.

This link coming with and token. You can add your parameters after “==” and run your code. And we get the result online..

Azure records all process that happened in monitor menu. By this property you can monitoring your process as well..

Result

We crated and deployed our function to the cloud and it is online right now. Of course this is quite simple. In next article we will connect our function with our database. That scenario is more suitable for real life.

I hope it helps you…

Keep learning and developing…