How to Develop Serverless Code Using Azure Functions?

chaitanya Last Updated : 30 Jan, 2023
6 min read

Introduction

Azure Functions is a serverless computing service provided by Azure that provides users a platform to write code without having to provision or manage infrastructure in response to a variety of events. Whether we are analyzing IoT data streams, managing scheduled events, processing document uploads, responding to database changes, etc. Azure functions allow developers to run a piece of code without having to provision or manage infrastructure in response to various events.

Scalability is built into Azure functions. When demand for execution increases, more resources are automatically allocated to the service, and when demand decreases, all extra resources and application instances are automatically decommissioned.

Azure Functions

Source: jiadongchen.com

 Learning Objectives:

In this article, we will

  1. Understand the basics of azure functions and how they are scaled and deployed.
  2. Learn about azure function features such as the pay-per-use pricing model, scalability, choice of languages, etc.
  3. Learn about hosting plans available for azure functions.
  4. Develop an azure function using C#, which will delete all the files from blob storage at 9:30 AM every day using Timer Trigger.

This article was published as a part of the Data Science Blogathon.

Table of Contents

  1. What are Azure Functions?
  2. Features of Azure Functions
  3. Triggers and Binding in Azure Functions
  4. Hosting Plans for Azure Functions
  5. Steps to Develop Azure Functions for a Scenario
  6. Conclusion

What are Azure Functions?

Azure Function is a trigger-based service that runs a piece of code or a script. As a serverless computing service, with the help of Azure Functions, users can run code without having to provision or manage infrastructure in response to various events.

Azure Functions are tremendous for stateless applications/solutions. Azure Functions provides users with a great choice of programming languages, such as Node.js, C#, Python, PHP, Java, etc., to write their code without worrying about any infrastructure configurations. Azure Functions automatically scale up and down based on the executions.

Azure Functions

Source:learn.microsoft.com

A function app consists of one or more functions that are scaled, managed, and deployed together. All functions in the same function app must have the same programming language, runtime version, pricing plan, and deployment approach.

Features of Azure Functions

Serverless Applications: Azure Function allows users to write the code without having to provision or manage infrastructure in response to various events.

Choice of Languages: Azure Function gives a variety of programming languages: C#, Java, JavaScript, Python, and PowerShell choice to the users.

Pay-per-Use Pricing Model: Azure Function allows users to pay based on the consumption of the Azure Function.

Scalable and Easily Upgradable: Azure Functions automatically scale up and down based on the executions.

Azure Functions

Source:learn.microsoft.com

Easier to Write and Deploy: Users can easily write and deploy Azure Function using Azure portal, Visual Studio, or Visual Studio Code.

Easily Connect to Other Services: With the help of triggers and bindings, Azure Function can easily connect to other resources or services like Logic Apps, Cosmos DB, Azure SQL, Blob Storage, Event Hub, etc.

Triggers and Binding in Azure Functions

Triggers are what cause a function to run. For example, A timer-triggered Azure Function runs based on the time of the timer object passed in the function.

Azure Functions

Source:www.serverless360.com

Binding provides a way of connecting another resource or service to the function.

Below are the two types of bindings:

1. Input Binding: An input binding is the data your function receives in the parameters. For example, Azure SQL input binding retrieves data from the Azure SQL database and passes it to the function input parameter.

2. Output Binding: An output binding is data sent by the function’s return value. For example, Azure SQL output binding lets you insert data into Azure SQL using Azure Function.

Hosting Plans for Azure Functions

Azure Function hosting plan decides how the function app is scaled, the resources available to the function app, etc.

microsoft azure

Source: hub.packtpub.com

Below are the hosting plans available:

1. Consumption Plan: It is the default hosting plan for Azure Function. Here, the function app is scaled automatically based on the load. Users have to pay only based on the consumption of the Azure Function.

2. Premium Plan: The premium plan provides more CPU or memory options.

3. Dedicated Plan: The dedicated plan controls scaling and computing.

Steps to Develop Azure Functions for a Scenario

Now, you are familiar with the concepts of Azure Function. Let’s see how we can build an Azure Function using C#, which will delete all the files from blob storage at 9:30 AM every day using Timer Trigger.

Follow the below steps:

1. Sign in to your Azure Account. Create a resource (+). Select Storage Account.

Azure Functions

Azure Functions

2. Enter the information in the requisite fields on each tab of the Storage Account.

Azure Functions

3. Click on Review + Create.

microsoft azure

4. After creating the Azure Storage Account, click on + Container. Provide demo-delete in the name and select Container in the public access level. Click Create.

microsoft azure

5. Now, in the demo-delete, upload one file. The file has been uploaded successfully.

microsoft azure

6. Create an Azure Function App deptCreate with a Function named “ProcessBlobData.”

microsoft azure

7. A timer-triggered Azure Function runs based on the time the timer object passes in the function. Now, open Visual Studio. Click File>New>Project> Search for Azure functions in the template. Click Azure Functions> Next. Provide the Project name. Click Next> Timer Tigger. Keep the default selected values and click Create.

creating new project

creating new project

8. After the project gets created, open the Function1.cs file and paste the below code snippet into the file:

            log.LogInformation($"Timer trigger function executed at: {DateTime.Now}");
            string blobConn = Environment.GetEnvironmentVariable("BlobConn");
            BlobContainerClient blobContainerClient = new BlobContainerClient(blobConn, "demo-delete");
            var blobs = blobContainerClient.GetBlobs();
            foreach (BlobItem blobItem in blobs)
            {
                blobContainerClient.DeleteBlobIfExistsAsync(blobItem.Name);
                log.LogInformation($"Blob Name {blobItem.Name} is deleted successfully.");
            }

code

9. Install the Nuget Package: Azure.Storage.Blobs

code

10. In the local.settings.json, paste the connection string for Azure Storage Account in the BlobConn.

code

11. Right Click on Solution > Publish > Azure Function App (Windows)> Next.

code

12. Now, select the deptCreate function app and click Finish.

azure functions

13. Run the function from Azure. We see that all the files got deleted.

azure functions

Conclusion

This article shows how users can write the code without having to provision or manage infrastructure using Azure Functions. We have seen how one can easily write and deploy Azure Function using Azure portal, Visual Studio, or Visual Studio Code. Azure Function can easily connect to other resources or services like Logic Apps, Cosmos DB, Azure SQL, Blob Storage, Event Hub, etc., with the help of triggers and bindings. Apart from this, we have seen how they automatically scale up and down based on the executions. Below are some major takeaways about Azure Functions:

1. We have learned about the function app and its features.

2. We deeply understood that we could connect other Azure services to Azure Functions using triggers and bindings.

3. We have seen how we can manage scaling, CPU, and memory options using Azure Function Hosting Plans.

4. Apart from this, we have learned to develop an Azure Function using C#, which will delete all the files from blob storage at 9:30 AM daily using Timer Trigger.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Full Stack developer with 2.6 years of experience in developing applications using
Azure, SQL Server & Power BI. I love to read and write blogs. I am always willing to learn new technologies, easily adaptable to changes, and have a good time
management, problem-solving, logical and communication skills

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details