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.
Source: jiadongchen.com
Learning Objectives:
In this article, we will
This article was published as a part of the Data Science Blogathon.
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.
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.
• 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.
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 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.
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.
Azure Function hosting plan decides how the function app is scaled, the resources available to the function app, etc.
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.
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.
2. Enter the information in the requisite fields on each tab of the Storage Account.
3. Click on Review + Create.
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.
5. Now, in the demo-delete, upload one file. The file has been uploaded successfully.
6. Create an Azure Function App deptCreate with a Function named “ProcessBlobData.”
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.
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."); }
9. Install the Nuget Package: Azure.Storage.Blobs
10. In the local.settings.json, paste the connection string for Azure Storage Account in the BlobConn.
11. Right Click on Solution > Publish > Azure Function App (Windows)> Next.
12. Now, select the deptCreate function app and click Finish.
13. Run the function from Azure. We see that all the files got deleted.
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.