How to Create Serverless Logic with Azure Functions

How to Create Serverless Logic with Azure Functions

What is Serverless Computing?

You can think of serverless computing as a function as a service, or a microservice that is hosted on a cloud platform.

In this setup, your business logic runs as functions, and you do not have to manually create or scale infrastructure. The cloud platform (Microsoft Azure in this case) manages the infrastructure. This implies that your app is automatically scaled out or down depending on the load.

Microsoft Azure has several ways to build this sort of architecture. The two most common approaches are Azure Logic Apps and Azure Functions, which we will focus on in this tutorial.

What are Azure Functions?

Azure functions are a serverless application platform. It enables developers to host business logic that can be executed without provisioning infrastructure.

Azure functions provide intrinsic scalability, and users are charged only for the resources that they use. It’s also important to note that you can write your function code in the language of your choice, including C#, F#, JavaScript, Python and PowerShell Core.

Support for package managers like NuGet and NPM is also included, so you can use popular libraries in your business logic.

By the end of this tutorial, you will be able to:

Step 1 – Create Your Azure Function App

To host business logic that can execute without provisioning infrastructure, you need to create your Azure Function app.

After you’ve created a valid and active Microsoft Azure account, you will then navigate to the Azure portal.

Click the Create a resource button.

In the menu, you will see that Function App appears. Click the Create button to create a function app. You will then see the Create Function App pane.

If the Function App button does not appear, select Compute in the Categories list and then select Function App in the pane.

At this point, enter the project details on the Basics tab before clicking the Review + create button.

The Subscription option may differ for you. It will depend on the Azure subscription you have available.

For the Resource group option, select a pre-created one if you are familiar with Azure and have one created. Else, create a new one using the Create new button.

A resource group simply implies grouping together similar services on your Azure account, so it makes it easier to manage.

For the Function App name option, enter a globally unique app name, which becomes part of the base URL of your service. Mine is named salim-freeCodeCamp-functions.

For the Publish option, select Code.

For the Runtime Slack option, select Node.js which is the language we use to implement the function examples in this tutorial. Leave the Version option as default.

Fill in the Region option with a geographical location closest to where you are. A region is a set of physical data centers that serve as servers. Since, I am in based in Nigeria, I selected South Africa North.

For the Operating System, it’s been recommended for you based on your selection of runtime.

For the Plan option, select Consumption (Serverless). The plan you choose dictates how your app scales, what features are enabled, and how it is priced.

At this point, you can then click the Review + create button.

The validation and deployment process usually takes three to five minutes, give or take. Once the validation and deployment processes are complete for the Azure Function, you can then verify that your Azure function app is running.

Step 2 – Verify That Your Azure Function App is Running

When the deployment process is completed, select Go to resource. The Function App pane for your function will appear. In the Essentials section, select the URL link to open it in a browser.

A default Azure web page will appear with a message that your Functions app is up and running.

Step 3 – Run Your Code On-Demand with Azure Functions

Now that you have created a function app, you will build, configure, and execute the function. To do these things, you need to understand two concepts – Triggers and Bindings.

Azure Functions are event-driven, so they run in response to an event. The event that starts a function is called a trigger, and a function must be configured with exactly one trigger.

Azure supports triggers for a range of services including:

A binding, on the other hand, is a declarative way to connect data and services to your function. Bindings interact with various data sources, which helps make sure you don’t have to write the code in your function to connect to data sources and manage connections. The platform takes care of that complexity for you as part of the binding code.

Each binding has a direction – your code reads data from input bindings and writes data to output bindings. Each function can have zero or more bindings to manage the input and output data processed by the function.

In all, a trigger is a type of input binding that can initiate execution of some code. Microsoft Azure provides many bindings to connect to different storage and messaging services.

To run your code on-demand on Azure Functions, you must create your function to run your code within the function app using the predefined template.  

To do this, click the Function tag on the menu bar to the left of your function app’s home page.

Then click the + Create button to create the function to be used using the templates.

Whilst leaving everything else as its default settings, you will select the Azure Queue Storage trigger template for this tutorial. The trigger will be run whenever a message is added to a specified Azure storage queue.

Click Create to create the function.

When you create a function from a template, several files are created, including a configuration file, function.json, and a source code file, index.js. Navigate to the Code + Test button on the left.  Select the function.json file in the dropdown. The code will take the form shown below:

Replace that code with the code in the block below:

At its core, this block of code simply implies that the function will be triggered when a message is added to the queue that’s named myqueue-items and the return value will be run into the outTable.

Save and then Test/Run the function.

Upon clicking the Test/Run button, you will see the page shown below.

Leave the key as the default host key but edit the body. Change it to the following input:

On running, you will see this output:

This implies that your function runs well as the result automatically appears in the Output tab. The image above is blank because there is in fact no business logic added to the function.

How to test your Azure function

In general, there are two ways to test your Azure function – manually and in the Azure portal.

What you just did is through the Azure portal. You can start a function manually by triggering the configured trigger.

For instance, if you’re using an HTTP trigger, you can use a tool, such as Postman or cURL, to initiate an HTTP request to your function endpoint URL, which is available from the function definition (Get function URL).

In this tutorial, you have seen that serverless computing is a great option for hosting business logic code in the cloud. You have seen that with serverless offers such as Azure Functions, you can write your business logic in the language of your choice.

Also, it is important to note that not only does the use of serverless computing solutions avoid the over-allocation of infrastructure (because they can be created and destroyed on demand), but they are also event driven. Event driven in the sense that they run only in response to an event (called a ‘trigger’), such as a message being added to a queue, or receiving an HTTP request.

Finally, I share my writings on Twitter if you enjoyed this article and want to see more.

This content was originally published here.