Azure Function - An Introduction
An Azure Function is a perfect example of serverless architecture or computes service. In this article, we discuss about the basic concept of Azure Functions.
Join the DZone community and get the full member experience.Join For Free
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.
Compare between Azure Functions and 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.
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:
- To implement Azure Function, we do not require to learn any new programming languages. We can develop it by using the existing skillset.
- Azure Function also fits into the existing deployment models using Visual Studio.
- Scalability is one of the major benefits of Azure Function. Azure Functions always uses compute-on-demand.
- Azure Function is Cost-Effective.
- 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.
- 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
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.
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.
Once the Function App deployment has been completed, then navigate the function app and it will display as below:
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.
Now, Our Function App is ready. We need to create our first Azure Function within this Function App.
Create Azure Function using Azure Portal
Now, Select the Function options from the left-side menu panel in the Function App section.
Now, we need to perform the below steps to create our first Azure Function -
- Click on Add Button
- Select Develop in Portal from the Development Environment
- Select HTTP Trigger Template from the Template List
- Provide the Proper Function Name
- Click on Add Button.
Once the function is deployed, then click on Code + Test options. It will provide us with 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.
Now, click on the Test/Run option to execute the result.
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.
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.
Opinions expressed by DZone contributors are their own.