Azure Function - An Serverless Architecture

Today, in this article we will discuss basic concepts of Azure Function. An Azure Function is a perfect example of Serverless architecture or computes service. Using Azure Function, we can run the event-based code without managing any infrastructure. Since the Azure function is a trigger-based service, so it always executes a script of a block of code in response to a variety of events. An Azure Function can also be used to achieve the decoupling, high throughput or response, reusability of code, etc. So, we will discuss the Azure Functions in all aspects in four parts. Today, in the first part of this article series, we will discuss the below topics,

  • An Overview of Azure Function 
  • Difference between Azure Functions and Web Jobs
  • Concept of ARM Templates and use an Azure Function.
  • Why we need to use Azure Function.
  • Create Azure Functions with Help of Azure Portal.
  • Create Azure Functions using Visual Studio 2019

Azure Function: An Overview

An Azure Function is known as a Serverless technology that is growing faster nowadays. With the help of Azure functions, we can create a serverless solution that provides us the benefits of write less code, less infrastructure maintenance, and saving in costs. So, despite invests much effort in the deployments and maintaining servers for the application, the cloud infrastructure like Azure provides us all the latest resources required to keep running the applications. In the case of Azure Function, we just need to worry only about the piece of code which we write to implement the requirement or functionality and the rest of the others like infrastructure, environment, etc. handles by the Azure function itself.

Azure Functions are the next steps in the concept of Platform-as-a-Service (PaaS). Azure Functions provide us the facility to execute some discrete unit of code or functions, in an extremely flexible, scalable, and cost-effective manner. 

Azure Function can be developed by using many languages like C#, Java, JavaScript, TypeScript, and Python. Azure Functions are scalable. Normally, Azure Function provides a “compute on-demand” pattern. It means, first we need to implement our business login as Azure Function. After that, when the demand for the execution of that particular code block increases, at that time more resources are automatically allocated to the service, and when the demand drops, then extra allocated resources for the application instances automatically drop.

Compare between Azure Functions & Web Jobs

In the case of any web-based application, one of the common requirements is to perform some background tasks like batch processing, schedule tasks, etc. But the main issue is that running these tasks on IIS will consume one of the threads dedicated to serving the application and the entire process can be interrupted by an app pool recycle. So, their fore we need to perform some tricks to push off app pool recycles until our task has been completed. That’s why we always preferred to run the job outside of the IIS. 

WebJobs were the first attempt to solve this problem. Developers can invoke a WebJob through a messaging system such as Storage Queues Or Azure Service Bus and have it completed the task during running the main application continuously on. WebJobs always have built-in triggers for some different events inside of Azure environments like storage queues, blobs, service bus queues, topics, and schedule triggers.

Now, Azure Functions take the basic concept from the WebJobs and expand the working capability in some interesting ways. So, first, Azure Functions enable a whole raft of new trigger types. So, now it is quite possible to trigger such things as Cosmos DB Change feed, Event Hubs, and WebHooks. As a Web Developer, the HTTP Trigger is one of the most interesting options. HTTP Triggers enable us to build an entire website or web application entirely with Triggers. 

The below tables shows the difference between the WebJobs and Azure Functions.

  Web Jobs Azure Function
Trigger Web Jobs can be configured in two ways – Triggered Web Jobs and Continuous Web Jobs. Azure Function can be triggered with any of the configured triggers, but it cannot run continuously.
Languages Support Web Jobs are supported by many languages like C#, F#, JavaScript, and More. Azure Functions are also supported by many languages like C#, F#, Python, JavaScript, Node.js, TypeScript, and More.
Deployment Web Jobs always run as a background service for App Services like Web Apps, API Apps, and Mobile Apps. Azure Function is a separate App Service that runs in an App Service Plan.


Concept of ARM Templates

If we want to deploy an infrastructure-as-code to an Azure environment, then Azure Resource Manager (ARM) templates are one of the ways to doing it simply and repeatedly. In this process, we can define the objects which we want with their types, names, and properties as a JSON file that can be understood by the ARM API. 

The main advantages of using ARM API are that we can deploy multiple resources together in a single unit of JSON file. In this file, we declare the types of resources, what name we want to use against those resources, and also define the properties of the resources. After that, ARM API either creates new objects which match these details or changes an existing object if the resource name has the same name and type and assigned the mentioned properties against those resources.

The ARM API deploys the resources to the Azure, but it does not deploy any code or applications onto those resources. For example, we can use an ARM template to deploy a virtual machine with SQL Server already installed but can’t use an ARM template to deploy or restore a database using an SSDT package. 

Why use Azure Functions?

So, despite the serverless benefits of Azure Function, there are some other benefits also which we can achieve while using Azure Functions. Some of the common benefits of the Azure functions are as below,

  1. To implement Azure Function, we do not require to learn any new programming languages. We can develop it by using the existing skillset.
  2. Azure Function also fits into the existing deployment models using Visual Studio.
  3. Scalability is one of the major benefits of Azure Function. Azure Functions always uses compute-on-demand.
  4. Azure Function is Cost-Effective.
  5. We can monitor the Azure Function by using App Insights. App Insights provide us the analytics on the function which additional telemetry code to improve the performance.
  6. Azure Function can be implemented as a Microservice approach. 

In the case of Azure Function, we have different templates that can be used in the below-mentioned scenarios,

  • HTTPTrigger - Trigger the execution of your code by using an HTTP request. 
  • TimerTrigger - Execute clean-up 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.
  • QueueTrigger - Respond to messages as they arrive in an Azure Storage queue.
  • EventGridTrigger - Respond to events delivered to a subscription in Azure Event Grid.
  • EventHubTrigger - Respond to events delivered to an Azure Event Hub.
  • 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. 

Azure Functions supports triggers, which are ways to start execution of your code, and bindings, which are ways to simplify coding for input and output data.

Create Azure Function using Azure Portal

Now, it's time to create our First Azure Function. So, in this section, we will discuss how to create an Azure function using the Azure Portal. Before creating an Azure Function in Azure Portal, we first need to create a Function App in Azure Portal. After that, we can create different types of Azure Function with that Function App.

Create Function App using Azure Portal

Step 1

First, we need to log in to the Azure Portal. Click on the Create a Resource option from the Dashboard. Then Select Function App options from the resource list.

Azure Function - An Serverless Architecture

Step 2

Now, in the Create Function App section, we need to provide the below options to create a function app,

  • Resource Group – Need to select an existing Resource Group or can create a new one.
  • Function App Name – It contains the name of the function app.
  • Publish - Need to select either Code or Docker Container. This option means in which environment we want to deploy our function app. For our demo purpose, we select Code.
  • Runtime Stack - Need to select which language we need to use to develop the Azure Function. We select .NET as the programming language.
  • Version – While selecting the Runtime Stack option from the dropdown, then the latest version related to that programming language automatically select in the version dropdown.
  • Region – We need to select in which region the data center we need to use.

After providing the above field values, we need to click on Create + Review Button. After that click on Create Button.

Step 3

Once the Function App deployment has been completed, then navigate the function app and it will display as below,

Azure Function - An Serverless Architecture

Step 4

Now, if we copy the Function App URL and paste it into the browser tab, then it will display that our Function App is already running.

Azure Function - An Serverless Architecture

Now, Our Function App is ready. We need to create our first Azure Function within this Function App.

Create Azure Function using Azure Portal

Step 1

Now, Select the Function options from the left-side menu panel in the Function App section.

Azure Function - An Serverless Architecture

Step 2

Now, we need to perform the below steps to create our first Azure Function,

  1. Click on Add Button
  2. Select Develop in Portal from the Development Environment
  3. Select HTTP Trigger Template from the Template List
  4. Provide the Proper Function Name 
  5. Click on Add Button.

 Azure Function - An Serverless Architecture

Step 3

Once the function is deployed, then click on Code + Test options. It will provide us a test environment for the Azure Function within the Azure Portal. Initial, it displays the default code template for the function which is a C# based function. If we want to change the code, that we can do.

Azure Function - An Serverless Architecture

Step 4

Now, click on the Test/Run option to execute the result.

Azure Function - An Serverless Architecture

Create Azure Function using Visual Studio

In the above section, we discuss how to create Azure Function using Azure Portal. Now, we will discuss how we can do the same job by using Visual Studio 2019.

Step 1

First, Open the Microsoft Visual Studio 2019 and then click on Create New Project options.

Step 2

In the New Project Template List, Select the Azure Functions Project Template and then Click on the Next Button.

Azure Function - An Serverless Architecture

Step 3

Now Provide the Project Name and Click on Create Button.

Step 4

Now, we need to select the Azure Function Templates From the available list and then Click on Create Button.

Azure Function - An Serverless Architecture

Step 5

Now Build the solutions and Press F5. It will execute the project and the result will be as below,

Azure Function - An Serverless Architecture

Step 6

Now, copy the marked URL and paste it into the browser and check the output.

http://localhost:7071/api/Function1

Azure Function - An Serverless Architecture

Step 7

Now, make the below changes in the URL to pass the name parameter,

http://localhost:7071/api/Function1?name=Debasis%20Saha

Step 8

After changing the above URL, the output change to below,

Azure Function - An Serverless Architecture

Conclusion

In this article, we discuss the basic concept of Azure Function. Also, we discussed the comparison between Azure Function and WebJobs. Also, we discussed the steps related to the create Azure Functions by used either Azure Portal or Microsoft Visual Studio 2019. In the next article of this series, we will discuss the Trigger and Binding of the Azure Functions. Any suggestions or feedback or query related to this article are most welcome.