What is Azure Functions? What are Triggers and Bindings? How to use it? Microsoft Serverless Platform.

Ufuk Aytaş
8 min readSep 20, 2021

In the previous article, we examined the issue of Serverless Architecture. You can access it from the link below.

Note: I will update this section as new articles become available.

So let’s start…

Today, We will talk about What is Azure Functions, what is Trigger, what is Binding, Azure Function pricing, Azure Functions Cold Start problem, and Azure Function creation in Visual Studio.

What is Azure Functions?

Azure Function is a serverless approach that allows us to easily run small pieces of code, created to accelerate and facilitate application development, and where infrastructure needs are completely managed by Azure.

Azure Functions is the FaaS (Function as a Service) service provided by Microsoft Azure. With the cloud infrastructure, scaling of your applications under load is done automatically.

Azure Functions are a serverless approach where the entire infrastructure is managed by azure, where developers only focus on the code they write.

In this approach, functions are made up of small pieces of code and need a trigger to run. (For example HTTP Endpoint, Timer, Azure Storage, and Azure Event Hub) These triggers could be an HTTP call, a timer, an incoming queue message to Azure Storage Queue, or Service Bus.

It has a stateless structure. The operating time of the functions is short (maximum 10 minutes). You are only paid for the time Azure spent running your code. If your function is in standby mode, you will not make any payments. Nuget and NPM support are available. We can create a function either through the Azure Portal or through Visual Studio Code or Visual Studio IDE. One of the strong features of Azure Function is that it supports different languages. You can review the languages and runtime versions it supports in the list below.

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

Azure Functions are also open source.

What is a trigger?

Triggers are events that are configured to call azure functions. Our Function needs a trigger to run, and a trigger can be defined for each function. It comes integrated with other Azure services, allowing us to integrate between different systems with little effort.

You can review the following list of triggers;

  • HTTP Trigger
  • Timer Trigger
  • CosmosDB Trigger
  • Blob Trigger
  • Queue Trigger
  • EventGrid Trigger
  • EventHub Trigger
  • ServiceBus Queue Trigger
  • ServiceBus Topic Trigger

What is Binding?

Bindings enable us to connect to data in different Azure services within the function. We can connect to more than one service within the same function. Normally, to connect to a service, we need to code for that service. To connect to these services with Binding, it will be sufficient to define attributes and enter the parameters requested from us. In this way, we can connect to the relevant Azure service and develop business logic from our function using ready-made objects.

There are two types of binding. These are called Input binding and output binding.

Input Binding can read data for the service we are connected to and process through them.

Output Binding is the processing of data by sending it to related services. You can also use bindings defined in systems such as SendGrid and Twilio.

In the meantime, you can write custom binding extends if you wish.

Sample Azure Services we can Bindings

  • Azure Cosmos DB
  • Azure Event Hubs
  • Azure Event Grid
  • Azure Mobile Apps
  • Azure Navigation Hubs
  • Azure Service Bus
  • Azure Storage

For more information about Bindings, you can check the link below.

Azure Function pricing.

There are three types of pricing models for Azure Function. These;

Consumption plan: In this plan, resources are managed and calculated by Azure. You are only paid for the time the code is running. We pay as we use it.

App Service plan: Your functions work continuously like web apps. Automatic or manual scaling can be done.

Premium plan: This plan allows you to develop responsive applications by avoiding the cold start problem. Unlimited function execution time is defined. (60 minutes guaranteed). It allows the creation of a virtual network connection.

For more detailed information about hosting plans;

Comparison of Consumption plan and Premium Plan.

Azure Functions Cold Start Problem.

We need infrastructure for our applications to work. In Azure Functions, all infrastructure needs are provided by Azure. Developers are not concerned with infrastructure requirements and problems. If our Functions are running under the Consumption plan, it will start with a few seconds delay on the first run. This is because Functions run in a shared environment. After the first call, they wait for the next call for 20 minutes. During this time, if there is no new call, it will start idle. If there is no call to the application, it switches itself to offline mode, waits until a new call is received, and becomes active again.

You can choose the App Service Plan or the Premium Plan to avoid cold tying. It should be noted that when we select these plans, we will work on selected sources for 24 hours and have to pay more.

If you wish, you can dockerize Azure Function and use it as a docker container.

You can find a detailed analysis of how long a cold start can be in this article.

Creating Azure Function in Visual Studio

After general information about Azure functions, let’s create a function in Visual Studio. I will create my example using Visual Studio 2019.

  1. Let’s choose the Azure functions project in Visual Studio.

2. At this stage, we need to select a trigger that will trigger our function from the left section. According to our selection, the information requested from us will change in the right section. In this example, we are selecting the HTTP Trigger.

3. After selecting HttpTrigger, we are asked to select a storage account. In the example, I selected the Storage Emulator I installed locally. At this stage, we can choose a Storage Account from our Azure account by clicking browse. The reason why he wants it Functions work data requires a storage account. Function information is stored under the storage account. If you wish, you can view the storage account details with “Azure Storage Explorer”.

It also asks us to choose the authorization level. We can choose multiple authorization levels for HTTP requests. In the example, I proceed by selecting the Anonymous authorization level.

Anonymous: It is used to make our function anonymously accessible.

Function: We can use it when we want to authorize it at the function level. We need to define a “Function Key” on the portal and send this key in function calls.

Admin: We need to define “Master Key” for this level. Unlike the Function authorization level, access to all functions in the application is provided.

In the example, we proceed by selecting the Anonymous authorization level.

When we create the Azure Function project, we encounter a structure like the one above. Here;

Function1.cs: There is the HttpTrigger function we have selected in the file.

host.json: There are general configuration settings of the application in the file.

local.settings.json Application settings, connection information is defined here.

If we examine an example Function;

  1. Function Name The field where we define the name of the function.
  2. The part where we define how the Trigger Function is triggered. The HTTP Trigger instance includes the authorization level, allowed HTTP methods, and Routing.
  3. The part where we process the data from Body QueryString.
  4. The part where the Response function returns a response.

When we run our function, we will see a screen like the one below on the console.

When we call the related function URL in the browser, the function will be triggered and a response will be returned to us. Since the function is triggered in Console, log output is loaded for triggering. In this way, we can check here that the triggering process was successful.

As you can see, we sent a parameter to our function and our code snippet worked and produced output for us.

Conclusion

Azure Function is a Serverless service developed by Azure that simplifies application development, eliminating infrastructure responsibilities, and thus focusing only on the code we write. This approach saves us from the fixed costs we pay for hosting our applications. We pay for the time our functions are triggered and run. With Azure Portal or Visual Studio, we can easily create Azure Function, test, and deploy my applications.

Please feel free to comment and contact me with your questions.

Twitter: https://twitter.com/ufukaytas
Linkedin: https://www.linkedin.com/in/ufukaytass/
Github: https://github.com/ufukaytas/

--

--

Ufuk Aytaş

Software Architect & Developer @CorendonDutchAirlines, Husband, Dad, Fishing, Coffee addict :) #Serverless #Cloud, #Azure, #Dotnet