This content originally appeared on Bits and Pieces - Medium and was authored by Lahiru Hewawasam
Running serverless microservice workloads on Azure
Serverless microservices are customizable cloud-based functions provided by cloud providers to perform specific operations. They are highly scalable, modular, and independent of the other functions running simultaneously within the application.
An application that runs on traditional monolithic architectures tends to have all its logic within a single unit.
In contrast, the modularity of the serverless microservice architecture allows these functions to occur independently from the rest. Furthermore, since the cloud provider maintains and looks after the underlying infrastructure, developers need only worry about developing and deploying these functions.
Microsoft joins in on the serverless microservices craze by introducing Azure Functions. So, this article will discuss the steps of developing serverless microservices with Azure Functions to give you a better understanding.
Using Azure Functions to Build Microservices
Azure Functions provide an easy and seamless platform for developers to publish and run serverless microservices without worrying about deployments or maintenance of its underlying infrastructure.
Azure Functions supports multiple languages, such as C#, JavaScript, and Python, allowing developers to build their Azure Function in their preferred language.
However, it is also important to remember that there are multiple versions of Azure Functions runtime available and each of these versions supports different languages.
Therefore, selecting your preferred language may need to include selecting a suitable runtime version. You may refer to the Microsoft documentation for a complete list of languages and runtime versions Azure Functions supports.
Building An Azure Function
You will need an active Azure subscription to build your first Azure Function. If you just want to try it out, you can use the free plan which provides a maximum monthly capacity of 1 million requests. However, the paid plans offer more powerful compute nodes to process more demanding enterprise-level workloads.
We will be using the free edition of Azure Functions to go through this example.
Step 1: Create an Azure Application
The first step in creating an Azure Function is creating and configuring a Function App. When creating the Azure Function App, you need to specify the basic configurations such as the subscription, name, and most importantly, the “Runtime Stack” that determines the language.
Currently, you can select one of the following Runtime Stacks:
- .NET
- Node.js
- Python
- Java
- PowerShell Core
- Custom Handler
Moving along with this example, we will use “Node.js” as our Runtime Stack and version “14 LTS”.
You can configure the rest of the configurations within the wizard according to your preference, which allows you to customize the deployment to suit your requirement better.
Basics
Subscription: Azure subscription for the billing of the Function App.
Resource Group: Specifies the resource group that this Azure Function App will belong; it is essential to note that resources within the same resource group share the same lifecycle, permissions, and policies.
Function App Name: Specifies the Azure Function App’s name and creates the address that the application is published. You can also specify custom domains.
Runtime Stack: Determines the language that the Azure Function and Function App will use.
Version: Determines the version of the runtime.
Region: This option specifies the Azure region that the Function App will run on.
Hosting
Operating System: Specifies the operating system of the underlying infrastructure; it is essential to note that Azure automatically selects the recommended operating system based on the Runtime stack selection.
Plan: This option specifies what features are available, how the Function App scales, and how the price calculation for the Function App takes place.
Networking
Networking: This option allows advanced network control over the Function App. The Function App is open to the Internet and cannot reach any virtual network within Azure by default. Standard and Premium subscription plans of Azure Functions allow you to configure advanced restrictions such as limited access to external networks and allow Azure Functions to communicate to virtual networks within Azure. However, these advanced features are not available within the free edition.
Monitoring
Application Insights: This function enables the users to configure Azure’s Application Performance Management (APM) service to monitor the application. It will automatically detect performance anomalies and provide advanced analytics to diagnose issues within the application. However, this feature is only available for certain subscriptions, runtime stacks, operating systems, publish types, regions, and resource groups.
Tag
Tag: This is an easy way of categorizing resources and building a consolidated view for billing. It is possible to apply the same tag to multiple resource groups or individual resources within multiple resource groups.
The next step is to finalize, review and create the Azure Function App, and then you are one step closer to creating your Azure Function.
Step 2: Create an Azure Function
You can create multiple Azure Functions within a single Azure Function App. These Azure Functions need a starting template to specify what types of actions can trigger each function. There are many predefined triggers to choose from, ranging from a simple HTTP trigger to a more complex trigger that integrates with native Azure services.
Depending on the trigger type, the integrations and additional configurations also vary to facilitate the necessary inputs for the function.
Also, the Azure Function supports a wide array of methods to save or forward to output, such as saving the response in Azure Blob Storage or even sending the response via Twilio’s SMS service.
Step 3: Configure the Azure Function
The final step is to code and deploy the Azure Function. Azure allows you to code the function on the Azure portal itself, thus eliminating the need for any IDEs. However, you can still use popular IDEs such as Visual Studio and Azure Functions SDK for more complex deployments.
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
? "Hello, " + name + ". This HTTP triggered function executed successfully."
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
context.res = {
// status: 200, /* Defaults to 200 */
body: responseMessage
};
}
This Azure Function allows the user to pass a variable called “name” and outputs the same in the response.
Overall Structure Setup
Microservice architectures differ from monolithic architectures, where applications do not consist of a single logic function. All functions comprise small autonomous services that are independent. This unique approach provides the application more flexibility in upgrades and improved in-service resilience failures, where a failure in one function will not interrupt the other functions or services.
Individual microservices usually communicate over lightweight APIs over HTTP/HTTPS, giving them tailored and flexible integration with other components and functions.
The independent nature of the architecture allows each function to be scaled individually. This way, the user can only scale up or down the functions’ resources that need it while the rest of the functions remain unchanged.
In usual serverless microservice architectures, an API gateway sits in front of the functions while allowing granular control of each function. It also allows for individual or multiple storage/output options for each function. In certain instances, you can use the same storage/output options for multiple functions.
Figure 03: Serverless Microservices Basic Architecture
Though there are numerous benefits of using serverless microservices, there occur some challenges when selecting the serverless microservices:
- Increased Complexity: Overall complexity of the application increases as each application gets more moving parts.
- Lack of Governance: Microservices provide an independent and decentralized mechanism to build each function. You can use a different language for each function. Even though it is beneficial, it also brings in the issue of not having a common standard. Therefore, each function needs special attention to adhere to proper governance controls.
- Network Congestion: Microservices may cause congestion as multiple individual functions attempts to communicate on the same network.
- Service Dependency Latency: If an application contains microservices linked to one another, then the network latency becomes an issue.
Conclusion
In this article, I explained how to create a serverless microservice with Azure Functions. The architecture explained in this article is the standard architecture that most serverless microservices tend to follow.
Although there are some challenges in moving to serverless microservices, we can easily overcome them and improve application architecture and performance by using serverless microservices.
I hope you have found this helpful. Thank you for reading!
Bit: Feel the power of component-driven dev
Say hey to Bit. It’s the #1 tool for component-driven app development.
With Bit, you can create any part of your app as a “component” that’s composable and reusable. You and your team can share a toolbox of components to build more apps faster and consistently together.
- Create and compose “app building blocks”: UI elements, full features, pages, applications, serverless, or micro-services. With any JS stack.
- Easily share, and reuse components as a team.
- Quickly update components across projects.
- Make hard things simple: Monorepos, design systems & micro-frontends.
Learn more
- How We Build Micro Frontends
- How we Build a Component Design System
- How to reuse React components across your projects
- Painless monorepo dependency management with Bit
Developing Serverless Microservices with Azure Functions was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Lahiru Hewawasam
Lahiru Hewawasam | Sciencx (2022-07-12T07:01:33+00:00) Developing Serverless Microservices with Azure Functions. Retrieved from https://www.scien.cx/2022/07/12/developing-serverless-microservices-with-azure-functions/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.