1
Demystifying Serverless Applications
When it comes to software development, we are living in incredible times. With the evolution of cloud platforms and the rise of modern technologies, being a developer nowadays is both a wonderful way to live and a challenging profession to follow. There are so many ways to deliver an application and so many innovative technologies to explore that we may fall into a vicious circle where we focus more on the technologies rather than the actual solution.
This chapter aims to present the serverless architecture and explore how you can use this approach to implement a microservices application. To achieve this, it covers the theory behind serverless and provides an understanding of how it can be a viable alternative for microservices implementation.
The chapter also explores how Microsoft implements Function as a Service (FaaS), using Azure Functions as one of the options for building microservices. Two alternative development platforms will be presented: Visual Studio Code and Visual Studio.
By the end of this chapter, you will understand the different triggers available in Azure Functions and be ready to create your first function.
Technical requirements
This chapter requires Visual Studio 2022 free Community edition or Visual Studio Code. During the chapter, the details about how to debug Azure Functions for each development environment will be presented in the topics. You will also need an Azure account to create the sample environment. You can find the sample code for this chapter at https://github.com/PacktPublishing/Practical-Serverless-and-Microservices-with-Csharp.
What is serverless?
When someone asks you to develop a solution, the last thing they usually care about is how the infrastructure will work. The truth is, even for developers, the most important thing about infrastructure is that it simply works well.
Considering this reality, the possibility of having a cloud provider that dynamically manages server allocation and provisioning, leaving the underlying infrastructure to the provider, might be the best scenario.
That is what serverless architecture promises: a model we can use to build and run applications and services without having to manage the underlying infrastructure ourselves! This approach abstracts server management entirely, allowing developers to focus on their code.
The first cloud solution provider that presented this concept was Amazon, with the launch of AWS Lambda in 2014. After that, Microsoft and Google also provided similar solutions with Microsoft Azure Functions and Google Cloud Functions. As we mentioned before, the focus of this book will be Azure Functions.
There are many advantages that we can consider for using serverless computing. The fact that you do not have to worry about scaling can be considered the main one. Additionally, the cloud solution provider maintains the reliability and security of the environment. Besides that, with this approach, you have the option to pay as you go, so you only pay for what you use, enabling a sustainable model of growth.
Serverless can also be considered a good approach for accelerating software development since you only focus on the code needed to deliver that program. On the other hand, you may have difficulty overseeing a considerable number of functions, so this organization needs to be well handed to not cause problems while creating a solution with many functions.
Since the introduction of serverless, various kinds of functions have been created. These functions act as triggers that are used to start processing. As soon as the function is triggered, the execution can be done in different programming languages.
Now, let us check whether functions can be considered microservices or not.
Is serverless a way to deliver microservices?
If you look at the definition of microservices, you will find the concept of delivering an application as loosely coupled components that represent the implementation of a business capability. You can build something like that with a couple of functions, so yes, serverless is a way to deliver microservices.
Some specialists even consider serverless architecture an evolution of microservices, since the focus of serverless architecture is to deliver scalability in a safe environment, enabling the possibility of a set of functions to independently be developed, tested, and deployed, which brings a lot of flexibility to the software architecture. That is exactly the main philosophy of microservices.
Let us imagine, as an example, a microservice responsible for authenticating users. You may create specific functions for registering, logging, and resetting passwords. Considering that this set of functions can be created in a single serverless project, you have both the flexibility of creating separated functions and the possibility of defining the purpose of the microservice.
The serverless project will naturally support integration with databases, messaging queues, OpenAPI specifications, and other APIs, enabling the design patterns typically needed for a robust microservice architecture. It is also important to mention that keeping microservices isolated, small, and preferably reusable is a best practice worth following.
Now that you understand that you can write microservices using serverless approaches, let us understand how Microsoft Azure presents serverless in its platform.
How does Microsoft Azure present serverless?
In 2016, Microsoft introduced Azure Functions as a Platform-as-a-Service (PaaS) offering designed to deliver FaaS capabilities. This option enables innovation at a scale for business transformation. Today, Azure Functions gives us the opportunity to power up applications using multiple programming languages, including C#, JavaScript, F#, Java, and Python.
One of the standout features of Azure Functions is its seamless integration with other Azure services and third-party APIs. For instance, it can easily connect to different Azure databases (from Azure SQL Server to Azure Cosmos DB), Azure Event Grid for event-based architecture, and Azure Logic Apps for workflow automation. This connectivity simplifies the process of building complex, enterprise-grade applications that leverage multiple services.
Over the years, the possibilities with Azure Functions have evolved. Today, we can even manage stateful workflows and long-running operations, using Azure Durable Functions. With this, you can orchestrate complex processes that can be executed in multiple function executions.
But Microsoft has not only created an environment for coding functions. They have also created a complete pipeline for developers, following the DevSecOps process that's now widely discussed and used in enterprise solutions. Developers can use tools such as Azure Pipelines, GitHub Actions, and other CI/CD services to automate the deployment process. You can also monitor and diagnose events in these functions using Azure Monitor and Application Insights, which facilitate troubleshooting and optimization.
The PaaS solution also enables different setups to adjust scalability and security aspects. Depending on the hosting plan you decide to set, you can have different scaling opportunities, as you can check here:
- Consumption plan: The basic and most cost-effective option to get started with Azure Functions. Ideal for event-driven workloads with automatic scaling.
- Flex Consumption plan: Offers rapid, elastic scaling combined with support for private networking (VNet integration).
- Dedicated plan (App Service plan): Suitable for long-running functions and scenarios requiring more predictable performance and resource allocation.
- Azure Container Apps plan: A solid choice for microservices-based architectures that use multiple technology stacks or require greater flexibility.
- Premium plan: Designed for high-performance scenarios with the ability to scale on demand, providing support for advanced features such as VNet, longer execution times, and pre-warmed instances.
In summary, Microsoft Azure delivers serverless FaaS through Azure Functions, offering a powerful, flexible, and scalable platform that enhances the development and deployment of serverless applications. By using Azure Functions, developers can build and maintain responsive, cost-effective solutions. Now, let us explore how to create an Azure function in the Azure portal.
Creating your first serverless app in Azure
There are not many steps for creating your first serverless app in Azure. You can do it in a straightforward process when using the Azure portal. Follow these steps to get started:
- Log in to the Azure portal. To do so, open your web browser and navigate to the Azure portal at https://portal.azure.com/. Sign in with your Azure account credentials.
- In the Azure portal, click on the Create a resource button located in the upper-left corner.
Figure 1.1: Creating a resource in the Azure portal
- In the Search services and marketplace window, search for Function App and select it from the search results. This service will also be presented in the Popular Azure services section.
- Click...