A Quick Guide to Azure Functions

A Quick Guide to Azure Functions

What is a function?

A function is a line(s) of code that is reusable and has a single responsibility. Depending on which language (C#, Java, Node, etc.) you are familiar with, the word is being used differently, sometimes function, method, subroutine, etc. But ideally, their purpose is one – do the same work repeatedly.

What is Azure Function, then?

You write a single function which is the entry point for execution. This will get clear when we move to practical implementation.

Remember the word “Single Responsibility”! This will play a vital role when we discuss more in detail.

Here are key differences to note:

FunctionAzure Function
Usually, when you write a function in your application, it is part of a more extensive application.Per best practices, the function code remains separate from your main application or module.
The code for the function gets deployed to the server along with the entire application (either as one application or multiple)The code for Azure Function gets deployed separately from the main application.
Function code and main application use the same hardware, i.e., server (of course not in all cases but usually this is the scenario).The Azure Function is serverless and hence gets deployed to unknown servers. In most cases, there is a way to host the azure function on the same server (to reduce cost).
This architecture is monolithic and requires additional effort to make it scalable.The architecture is out-of-the-box scalable and does not require additional effort.

Now that we know the difference between traditional application (function) vs. Azure Function, let’s concentrate only on Azure Function.

What is serverless?

“Serverless” means a code that runs without any server. If there is no server (i.e., computer), where will the code run? Typically, when you deploy any application, you need a server, and the azure function is no different. In the general scenario, you know which server it is and where it is located. In the case of Serverless, you don’t. You do not need to; the cloud provider takes care of that on your behalf. The provider will decide which server to use and how to deploy your application.

However, there is a way to host the azure function on the same server to reduce costs. Here’s where hosting options come into the picture. You can host your function in two ways:

  • Consumption plan
  • App service plan

How to achieve Serverless on Microsoft Azure?

Azure Function Example

Let’s create our first azure function to understand more about it. Here is a quick guide to creating Azure Function using different tools. Primarily there are two ways:

  • You can create an azure function directly from the Azure portal
  • You can write azure function code locally in your favorite language and then deploy it to the Azure Function service. This approach gives you a lot of flexibility in terms of management and implementation.

Let’s get started with Visual Studio (I am using C#) –

  1. Create new Azure Function Project, File > New > Project
  2. Ensure “Cloud” is selected on the left sidebar and then select Azure Function
Core development

If you do not see this option, you will have to enable the “Cloud Development” feature in the visual studio installer.

3. Name your function appropriately – I have named it “HelloFunction.”

4. Select HTTP Trigger, for now; we will explore others options in an upcoming blog post.

Http Trigger
  • Azure Web Job Storage: Azure Web Job Storage is a connection to your storage account where logs and monitoring information for your azure function will be written. Following options are available:
  • None – No logs and monitoring information will be saved
  • Storage Emulator – This allows you to use a storage emulator on your local machine. Here is a complete guide on storage emulators.
  • Browse – This option will allow you to connect to your Azure account and fetch details of the storage account you select.

Recommendation:  

Ideally, it would be best to use “Storage Emulator” during development and “Azure Storage Account” when functions are hosted on Azure service.

  • Access Rights

Since we are creating an HTTP function, this function can be invoked by making an HTTP call. Ideally, when we develop an HTTP API, we secure them by enforcing security policies (Authentication, Authorization, Application/API Gateway, etc.). Following options are available:

  • Function – “Function” setting expects function level access key being passed to an HTTP request when called using authorization header. 

Note: It is not a good idea to pass the function key over publicly exposed APIs. Other techniques such as custom authentication, API gateway etc shall be used to make the APIs more secure.  

  • Anonymous – Anyone having access to a link can call the API. We should never make an HTTP API anonymous. This is a massive security breach to your backend unless intentionally done.
  • AdminSimilar to the “Function” access level, the “Admin” access level requires an admin key known as the “_master” key. This key provides additional access rights, such as managing the function app using REST API.

Let’s review the code by testing the following :

  1. Is the function name correct? You can have a Function name different from the actual function. The function runtime will consider the name given in the “FunctionName” attribute as the actual name for the function.
  2. Is the trigger of type “HTTPTriger” indicating the function can be invoked using an API call?
  3. Is the authorization level set for this function? It is set to anonymous for now.
  4. HttpRequest is an incoming message type for this function.
  5. Since HttpRequest is an incoming message type, it cannot be used by the application directly. But the message can help you read information from a query string or body depending on your need.
  6. Is it the function file we created?
  7. Host.json file is a configuration file deciding how the function should run.
  8. Local.setting.json is a setting or config file where configurations for applications are stored.

Let’s run the function – 

1. To run – “F5” as usual

Run Function

2. Use the URL to call the API from browser OR Postman

TracceWiter

The logs can be seen in the console. These logs are coming from “TraceWriter”.

Here is another example of one such HTTPTrigger function which I used for integrating SendGrid incoming message parsing email.

EmailParsing
  1. The function is anonymous because it is going to be called by a third-party provider.
  2. The function is HTTPTrigger
  3. Incoming message data (body and files) will be saved under “App_Data.”
  4. sendGridEmailParsing service will parse the incoming request.

Few things to note here:

  1. Keep the infrastructure part separate from the leading service. i.e., downloading and saving images is not part of the parsing service. This may change differently depending on if moved to a non-azure function.
  2. The exception is being handled since it was a requirement, but you can ignore it since if you make exceptions, azure runtime will assume that it worked fine and no need to perform a retry.

Azure Function v1 vs. v2

The azure function currently supports v1, v2, and v3. It is crucial to compare v1 and v2 because there are significant differences between the two than v2 and v3. I am writing these from the perspective of .NET technology.

Azure Function v1Azure Function v2
Supports .Net framework 4.7 and later. This means that if you have existing libraries or packages developed on .NET Framework, it will not require additional efforts to add Azure Function.Supports .NET Standard 2.2. I.e., If you are starting from scratch, Azure Function v3 should be your preferred choice. But if you have an existing application developed on .NET Framework, you will have to go with v1 or else and port your code to .NET Standard.
Dependency injection is not available out of the box. Substantial effort is required to add dependency injection.Dependency injection is available out of the box. This helps write better code.

We hope now you have some knowledge about various Azure functions and how you can leverage them. In case of queries, do get in touch with a DEV IT expert here.

The following two tabs change content below.

Hiran Desai

Solution Architect at Dev Information Technology Ltd.
I'm .NET full stack developer having experience in Enterprise Solutions, Web Application, Desktop application, Windows Services, Web Services, WCF & SOA, Javascript & jQuery (lover), MS SQL, Postgres, Oracle, Cloud Technologies and many more. I am a technology enthusiast and an active contributor in Research & Innovation department of DEV IT. I am a sport's junkie (especially cricket), addicted to video games, a wanderer and love staying back at home and relax.

Latest posts by Hiran Desai (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *