What Is Azure Functions: A Beginner's Tutorial

Azure Functions is a serverless concept of cloud-native design that allows a piece of code to be deployed and executed without needing server infrastructure, web server, or any configurations. In addition, azure functions can be written in multiple languages such as C#, Java, JavaScript, TypeScript, and Python.

This article explains what an azure function is, how to create an azure function, and how to debug, test, and deploy azure functions.

Azure functions are scalable. When demand for execution increases, more resources are allocated automatically to the service, and when requests fall, all extra resources and application instances drop off automatically.

Azure functions

Let's say you have to send a birthday email to your customers. You're an ASP.NET web developer. Instead of building a website in ASP.NET and employing and hosting it on IIS just for one feature, you can write an azure function, put your email login in the function, and deploy it on the Azure cloud. The azure functions will directly connect to your data source, get your customers' emails, and send them an email on a scheduled date and time.

Azure functions are best suited for smaller apps with events that can work independently of other websites. For example, some azure functions are sending emails, starting back up, order processing, task scheduling such as database cleanup, sending notifications, messages, and IoT data processing.

Why use Azure Functions

Here are some of the reasons why you should use azure functions. 

  1. Azure functions are lightweight and serverless.
  2. Azure functions are easier to write and deploy.
  3. Azure functions are fast to execute because there is no large application, startup time, initialization, and other events fired before the code is executed.
  4. Azure functions' execution is triggered when an event is fired.
  5. Azure functions are compute-on-demand, and that is scalable. When the demand for the execution increases, more resources are allocated automatically to the service, and when requests fall, all extra resources and application instances drop off automatically.
  6. Azure functions support programming languages, including ding C#, F#, Java, JavaScript, TypeScript, and Python. You choose your choice of language.
  7. Azure functions do not need any infrastructure and have 0 maintenance.
  8. The azure function can be built, tested, and deployed in the Azure portal using a browser.
  9. Azure functions are easy to upgrade and don't affect other parts of the website.
  10. Azure functions use industry standards and can communicate with other APIs, databases, and libraries.

Create an Azure Functions using Visual Studio 2019

Before we go too far, let's create Azure Functions using Visual Studio 2019.

Open your Visual Studio 2019 and create a new project.

On the new project template page, search for azure functions and see a template for Azure Functions. See below.

Create azure functions

Select the Azure Functions template and click Next.

On the below screen, you need to give your functions application a name and select a location where the VS project is goiwille.

Configure Azure functions in Visual Studio

You may also see from the above screen that the function is created using C# and have Ahas and Cloud tags means, which will be deployed and run on Azure cloud.

Click Create button.

On the next screen, we need to select what kind of application it will be. Azure functions have this concept of triggers. A trigger is what causes a function to run. Each function has precisely one trigger.

Let's take a look at the below screen that shows various triggers available in Visual Studio for an Azure functions app.

Create Azure functions app

As you can see, each trigger has its spic purpose.

For now, let us select Http Trigger that is executed whenever an HTTP request is made.

Click on Create button to create our app.

We land in Visual Studio with a default file, Function1.cs, and the default function code.

Azure functions app code

Here is the complete code of my function.

using System;    
using System.IO;    
using System.Threading.Tasks;    
using Microsoft.AspNetCore.Mvc;    
using Microsoft.Azure.WebJobs;    
using Microsoft.Azure.WebJobs.Extensions.Http;    
using Microsoft.AspNetCore.Http;    
using Microsoft.Extensions.Logging;    
using Newtonsoft.Json;    
namespace HelloFunction {    
    public static class Function1 {    
        [FunctionName("Function1")]    
        public static async Task < IActionResult > Run(    
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) {    
            log.LogInformation("C# HTTP trigger function processed a request.");    
            string name = req.Query["name"];    
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();    
            dynamic data = JsonConvert.DeserializeObject(requestBody);    
            name = name ?? data?.name;    
            string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." : $ "Hello, {name}. This HTTP triggered function executed successfully.";    
            return new OkObjectResult(responseMessage);    
        }    
    }    
}

That was the default code of our Azure functions.

Now, let's build and execute it.

Visual Studio provides an Azure Storage emulator to run and test azure functions locally.

Build and Run the application using F5.

You will see the Azure Storage emulator is being started.

Azure Storage emulator

The next screen will be func.exe show, ng this message that your function is ready and can be used.

Azure Functions Core Tools

As you can see from the above screen, the Azure functions, Function 1 has GET, POST and the URL is http://localhost:7071/api/Function1. This is the URL you can use to execute the function.

Let's type this URL in our local browser and pass a query string with a value. Then, the output looks like the following: the function takes the query string input and displays it on the screen.

Azure functions in browser

That is all. You just completed the Hello Azure Functions application.

Publish Azure Function to Azure

You can publish directly h your Azure functions app now in Visual Studio or Visual Studio Code.

In my Visual Studio project, right-click on the project name and select Publish.

It will launch the Publish screen, where you may select Azure, Docker, or other folders where you may wish to deploy your app.

In our case, I select Azure.

Publish Azure functions

On the next screen, you will need to select Azure function app operating system, Windows, Linux, or App Container.

I select Windows.

Azure functions app Windows

On the next screen, you will have to provide the name of the function app, an Azure subscription, Resource group, Plan Type, Location, and Azure Storage to deploy and execute an Azure functions app.

Azure functions settings

Click on Create button will create a new App Service.

Azure functions app service

This process may take a while. Wait until it's done and you see the below screen.

On the below screen, you will see the Function instance. Click on the Finish button.

Azure functions publish

Once finished, you will go back to the Publish page, where you can see the Site URL, configuration, and a User name and Password automatically assigned to the functions app. You may want to COPY the Site URL for later use.

Azure functions publish settings

Click on Publish button.

The Publish process will start.

Azure functions publish status

Once the publishing is finished, you will see Publish succeeded message.

Azure functions publish success

To ensure that the function app is published, you can now copy the Site URL into a browser. My URL was this,

https://hellofunction20210205221452.azurewebsites.net/

You ..will see a message that your functions app is up and running.

Azure functions test

Now, you're ready to consume this function app.

When to use Azure Functions?

Azure functions service is a lightweight, serverless compute service with its own use. You can't replace a large website with Azure functions.

Here are some of the use cases of Azure functions, 

  • Scheduled Tasks
  • Reminders and Notifications
  • Lightweight Web API

Azure functions are best suited for smaller apps with vents that can work independently of other websites. Some standard azure functions include sending emails, starting backup, order processing, task scheduling such as database cleanup, sending notifications, messages, and IoT data processing.

What are the benefits of Azure functions?

Azure functions have the following benefits

  • Azure .functions app is lightweight and requires few forces to deploy and execute.
  • Azure functions app us serverless and does not require any Web server setup in the cloud. the the 
  • Azure functions app is compute-on-demand and doesn't consume resources when not running.
  • Azure functions app charges are pay per usper-useou don't pay anything if you are not using.
  • Azure functions app is event event-drive executes only when the event is fired.
  • Azure functions app is independent of other apps and does not affect or interfere with other apps.
  • Azure functions app is easy to write and deploy.
  • Azure functions app is easy to maintain and support.
  • The Azure functions app is industry standard, developed, and consumed using industry-standard language and technologies.

What languages can be used for azure functions?

The languages supported to developer azure functions are C#, JavaScript, F#, Java, Powershell, Python, and TypeScript.

Where are Azure functions in the portal?

Azure functions in Azure are created using Functions App. Go to your Azure Portal and search for "Functions App." See below:

Azure functions in Azure portal

Click on Functions App to take you to the Function App page, where you can use the + Create a link to create new Azure functions.

Azure Function app in Azure

What tools are used to develop Azure functions?

The popular tools to develop azure functions are Visual Studio, Visual Studio Code, and CLI. However, you can use the tool or IDE that you used to write and build your web applications as long as they support one of the azure function languages C#, JavaScript, F#, Java, Powershell, Python, and TypeScript.

You can also use the Azure portal to develop azure functions in the browser without using any tool or IDE.

Learn here how to create an functions in Azure portal.

How to develop Azure functions?

You can develop azure functions using Visual Studio, Visual Studio Code, any developer IDE, or even the Azure portal. Azure portal and Visual Studio are probably the easiest way to develop azure functions.

Watch this video and learn how to build azure functions using Visual Studio Code. Asavari Tayal, Microsoft Program Manager, in the, is video, talks and demos what Azure functions are, how to build your first serverless Azure function code, and how to build, test, and deploy it in the cloud.

How to create and execute Azure functions

You can create azure functions using any developer IDE such as Visual Studio or Visual Studio Code. As l as the IDE supports azure functions. Visual Studio provides an Azure functions template.

Another easier way to create azure functions is using the Azure Portal. The Azure Portal has an add-on called Functions App. Search the Azure Portal for "Functions App."

Once an Azure FunctiFunctioneated, you need to deploy it in the Azure cloud. That is where it is hosted and gets executed (runs) on-demand. All the resources, such as the  Compute and Storage, are available in Azure and consumed as needed by the function.

Here is a details article on how to create an azure functions in azure portal.

How to debug Azure functions?

Debugging your code is an importintegralof software development. Watch this video to learn how to debug Azure functions using Visual Studio Code. The same debugging process can be used in Visual Studio.

How to test Azure functions

Once an Azure functions code is written on your local machine or in the Azure cloud, you should test them. If developed locally, you can write unit tests like any other .NET application.

Watch this video to learn how to create a test project for an Azure Function.

How to deploy Azure functions

Deploying Azure functions in Azure is pretty simple and straight forward. You can use either Visual Studio Code or Visual Studio to deploy your Azure functions in a few clicks.

Watch this video to learn how to deploy Azure Functions using Visual Studio Code.

How to use Azure functions and secure configuration with Azure Key Vault

In thiThis, lear teacher uses the secure configuration for Azure functions with Azure Key Vault.

Summary

Azure Functions is a serverless, compute-on-demand, lightweight, and independent app. In this article, we learned what the azure functions app is, what is benefits are, and when not to use azure functions. We also learned how to create an azure functions app using Visual Studio follow and how to debug, test, and deploy azure functions in the Azure cloud.

Next > Learn How to Create a C# Azure Function Using Visual Studio 2019 

References

https://docs.microsoft.com/en-us/azure/azure-functions/


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.