Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Building Serverless Apps with Azure Functions and Cosmos DB: Leverage Azure functions and Cosmos DB for building serverless applications (English Edition)
Building Serverless Apps with Azure Functions and Cosmos DB: Leverage Azure functions and Cosmos DB for building serverless applications (English Edition)
Building Serverless Apps with Azure Functions and Cosmos DB: Leverage Azure functions and Cosmos DB for building serverless applications (English Edition)
Ebook941 pages9 hours

Building Serverless Apps with Azure Functions and Cosmos DB: Leverage Azure functions and Cosmos DB for building serverless applications (English Edition)

Rating: 0 out of 5 stars

()

Read preview

About this ebook

This book provides examples to start with Azure functions and Azure Cosmos DB. It demonstrates the features available in both of the mentioned Azure services and discusses them in detail with some real-world examples. Reading a csv file and write to a Cosmos DB table store, Read emails using Microsoft Graph API and save them in a Cosmos DB, Cosmos DB trigger function to send SMS notifications to clients, A queue trigger to create new nodes in the Cosmos DB graph data store are some of them. You will be able to see the above case studies with code samples implemented in C# .NET Core, TypeScript, and Python. It consists of a very basic example, two intermediate samples, then and an advanced level one.

You will experience the triggers and input/output bindings available for a function, like queue trigger, blob trigger, and Cosmos DB trigger to name a few. Also, you will be able to see some interesting features available in Azure functions like performance optimizations, scalability of a function app, geographical distribution of the function in different locations, error handling, writing unit tests for the functions to avoid breaking changes, how to ensure a function app is secure, and then how to deploy a function, and monitor and troubleshoot a function app.

At the end of this book, you will gain strong experience in using Azure functions and how to manage serverless applications seamlessly without any failure with utmost performance.
LanguageEnglish
Release dateFeb 5, 2021
ISBN9789389328394
Building Serverless Apps with Azure Functions and Cosmos DB: Leverage Azure functions and Cosmos DB for building serverless applications (English Edition)

Related to Building Serverless Apps with Azure Functions and Cosmos DB

Related ebooks

Computers For You

View More

Related articles

Reviews for Building Serverless Apps with Azure Functions and Cosmos DB

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Building Serverless Apps with Azure Functions and Cosmos DB - Hansamali Gamage

    CHAPTER 1

    Beginning Azure Function Apps

    Introduction

    In the first chapter of this book, we are going to talk about the Azure serverless architecture and why we should consider using this architecture in our applications. You will then go through some real-world business scenarios in the next section of the chapter.

    This chapter will give you a basic idea of all the services in the serverless platform such as Azure Functions, Logic Apps, Event Grid, and Cosmos DB. Then it will walk you through different paradigms such as applications hosted in on-prem, Iaas, PaaS, and serverless applications.

    Then, it will focus more on the main topic of the book: Azure functions. It will talk about the features of functions, different service integrations on functions such as Cosmos DB, Event Grid, Storage, and Notification Hub. After that, you will be able to get a better viewpoint of the released and experimental languages; and functions supported with different runtime versions.

    You can learn about the heart of the function app, triggers and bindings in the next section of the chapter. In the end, you will get an idea of the different pricing options to host a function app and check the pros and cons of it.

    Prerequisites

    It would be better to have these prerequisites before going through this chapter:

    You should have an Azure subscription to try out the sample applications demonstrated in the chapter.

    Experience and understanding of different Azure services

    Experience in the C# or JavaScript languages

    Structure

    Azure serverless architecture

    What is Azure serverless architecture

    Why serverless architecture

    Real-world case studies of serverless applications

    A single page web application

    Web application backend

    Mobile application backend

    IOT backend

    Real-time file processing

    Real-time stream processing

    Automating tasks

    SaaS Integration

    Azure serverless platform

    Azure Functions

    Logic Apps

    Event Grid

    Before it is serverless

    Applications in on-prem

    IaaS

    PaaS

    Serverless applications

    First s teps with Azure function

    What is an Azure function?

    Features of Azure functions

    Service integrations

    Azure Cosmos DB

    Azure Event Hub

    Azure Event Grid

    Azure Notification Hub

    Azure Service Bus

    Azure Storage

    Blob storage

    Azure file storage

    Queue storage

    Table storage

    Azure Logic Apps

    Azure SignalR

    Microsoft Graph

    Power BI

    Twilio API

    SendGrid API

    Language support for functions

    C#

    F#

    JavaScript

    Java

    Python

    TypeScript

    Php

    Batch

    Bash

    Powershell

    Triggers and bindings

    Triggers

    HttpTrigger

    TimerTrigger

    CosmosDBTrigger

    BlobTrigger

    QueueTrigger

    EventGridTrigger

    EventHubTrigger

    ServiceBusTrigger

    SendGridTrigger

    GraphAPITrigger

    NotificationHubTrigger

    SignalRTrigger

    TableStorageTrigger

    Bindings

    Azure Storage

    Blob Storage

    Queue Storage

    Table Storage

    Cosmos DB

    Event Grid

    Event Hub

    Http and Webhook

    Microsoft Graph

    Mobile Apps

    Notification Hub

    SendGrid

    Service Bus

    SignalR

    Timer

    Twilio

    Type of functions

    Serverless web applications

    Serverless mobile backend

    Timer-based processing

    Real-time file processing

    Real-time stream processing

    Real-time bot processing

    Pricing in Azure functions

    Consumption plan

    Function scaling in consumption plan

    Billing in consumption plan

    App Service Plan

    Function scaling in app service plan

    Billing in app service plan

    When to use app service plan

    Objective

    You will get an understanding of Azure serverless architecture and different application deployment paradigms like IaaS, PaaS, and serverless.

    You will get an understanding of the Azure functions and their features. Moreover, the services that you can integrate with functions such as Cosmos DB, Event Hub, Service Bus, Storage, and Logic Apps and third party services such as Twilio API and SendGrid.

    Then you will get an idea of language support for functions in different runtime versions along with experimental languages.

    Triggers and bindings on an Azure function are the primary building blocks in a function app; you will get a basic idea of them as well.

    Finally, you will cover an important part when deciding to move your code to an Azure function which is pricing plans. You’ll get an idea of different pricing plans available, their features, and when to use which plan.

    Azure serverless architecture

    In the first section of this chapter, let’s see what Azure serverless architecture is, why we need to go for serverless architecture, and what are its benefits. Then, let’s see some real-world scenarios where Azure serverless architecture is used and try to understand the applicability of it in our business domain.

    Let’s explore Azure serverless platforms such as Azure Functions, Azure Logic Apps, and Azure Event Grid in detail and their features. You will get a basic understanding of these three Azure services under the serverless architecture. This book is going to give you a high-level understanding of the Azure functions using hands-on demos and real-world business scenarios.

    The final section of this chapter describes the application deployment paradigms from applications deployed in a physical on-prem server, and then Infrastructure as a Service (IaaS) which provides a virtual environment to deploy your applications through virtual machines or hyper-v without worrying about the scaling and resource management. After IaaS, it talks about Platform as a Service (PaaS) that provides an environment to host your application without worrying about the operating system updates or security patches.

    If you want to deploy a .NET web application, you will get all the required packages and libraries to host your application in this container and there is no need to install them manually. In the end, we will explore the latest serverless architecture after walking through all the paradigms and exploring its features. The serverless platform is a microservice-based architecture that focuses on application development instead of the platform. You can focus only on the application. There is no need to think about the host it’s running. When it comes to the pricing, in previous paradigms, you had to pay for the continuous running application. But in serverless code, you only pay for what you use in the application and only for the time the application runs.

    What is Azure serverless architecture?

    Azure serverless architecture focuses on developing applications faster without worrying about the application infrastructure. So, we don’t have to handle server or infrastructure. There is no need to worry about the operating system, its infrastructure, or configurations. We can only focus on the function implementation; in fact, server management and capacity planning is invisible to us and handled by Azure. Serverless code is event-driven; it works with triggers and bindings. Let’s talk about triggers and bindings in detail in the later stages of this chapter.

    Why serverless architecture

    Let’s see why serverless architecture is essential in real-world business applications. What are the features in that architecture and what is their usage?

    Build apps faster using serverless functions.

    There is no need to handle server or infrastructure and there are no administrative tasks.

    We don’t have to worry about the operating system, its configurations

    We can focus only on the function implementation and business logic.

    Applications can be scaled on demand without worrying about the infrastructure.

    Applications have high availability.

    Service billing is based on pay as you go: the number of resources consumed or the time our code is running.

    Create modern applications with real-time triggers and bindings

    Integrate Azure services such as Cosmos DB, Logic Apps, or else, use third-party services such as SendGrid and Twilio SMS API

    Work with intelligent services such as Azure machine learning and cognitive services

    Real-world case studies of serverless applications

    We have understood the features of serverless code and why we should go for a serverless architecture in the previous sections. Let’s explore some real-world case studies that used serverless code in their applications.

    A single-page web application

    We can create a SPA with a REST API service backend and deploy it into Azure. Single Page Application (SPA) will dynamically load the content to the page instead of a full-page load from the server. Angular, React, Vue, and, Ember JS are the most popular SPA frameworks to build web applications. In this way, you can use an Azure function to host your site and its pages and App calls functions using a webhook url and process its data. If we deploy a web application in a serverless environment, it will reduce the deployment steps and there will be no need to enable Cross-Origin Resource Sharing (CORS) for your site. In CORS, we can access a resource available in another domain that is other than the same domain we are accessing from. Most browsers serve the requests from the same-origin policy to prevent cross-site scripting attacks; we can use a proxy server or a proxy service to access a resource available in another domain via CORS.

    Webhook url: Webhook url is a user-defined http callback. It is an http url that triggers when a specific event occurs. When the trigger event occurs in one place, the webhook captures the event, collects the data, and sends that data to the url mentioned in the webhook.

    CORS: Web application will serve a cross-origin http request when it is trying to request a resource that is in a different origin, maybe in a different domain, protocol, or a port. In CORS, we have to add additional http headers to the request and let the application have permission to access a selected resource running in a different origin.

    Web application backend

    You can create a serverless backend to process a queue; or else, to write some output to a database, Azure SQL database, or a CosmosDB.

    Mobile application backend

    You can create a mobile application backend using an Azure function. We can call an http trigger and process the function to save data, send an email notification, or to process some high intensive task.

    IoT backend

    IoT devices can be used to send a message and process the message using the Azure function. The function app can be used to update a record in a CosmosDB or else to store data in blob storage.

    Real-time file processing

    We can use an Azure function to process the uploaded pdf files in blob storage. Then the files can be processed using OCR detection and sent to a database.

    Real-time stream processing

    When an application or a device produces a massive amount of data and stores inside an Event Hub, we can process it using an Azure function and send it to a CosmosDB for future use.

    Automating tasks

    Let’s say your application is going to send an email to its customers. To avoid sending multiple emails for the same customer, we can use an Azure function to clean up the database in a specific time interval.

    SaaS integration

    We can create a webhook to automate workflows in a Software as a service (SaaS) based application. As an example, we can save a file in OneDrive. It triggers a function that uses Microsoft Graph and modifies an excel sheet or draws excel charts based on the data.

    Azure Serverless platform

    The Azure Serverless platform includes Azure Functions, Azure Logic Apps, and Azure Event Grid. These services connect with other resources available in Azure. The serverless platform works with databases, storage, IoT devices, and more advanced features such as business analytics, machine learning, and artificial intelligence. We can use the Azure Serverless platform inside our application or else, go for a hybrid solution that integrates with other cloud resources and on-premises services.

    Azure functions

    The Azure Function is invoked by a trigger; it can be an http endpoint or a timer. The function can be integrated with other Azure services such as queues, storages, databases, or else with an Azure logic app. There are three versions of a function app. The first version (v1) is supported by .NET full framework. The other version (v2), supports for .NET Core and is based on the cross-platform .NET Core version that is compatible with macOS or a Linux environment. The latest function v3 version is generally available at the time of writing this book. We can use C#, F#, JavaScript, or experimental languages such as Java, Python, PHP, TypeScript, Batch, Bash, or PowerShell to write a function. We can also use the Azure portal to write a primary function or else, an IDE such as Visual Studio or Visual Studio Code. You don’t have to start from the beginning to develop a function app; you can use existing function templates.

    Logic Apps

    Azure Logic Apps provides a serverless platform to build automated workflows to integrate applications and data between cloud services or on-premises systems. You can build workflows using a visual designer available in the Azure portal or else, can pick up an existing trigger template. You can trigger created workflows based on an event or a timer as per the requirement. Once the trigger is created, the logic app offers many add-ins such as conditional statements, loops, steps, actions, or parallel processors.

    A logic app can be integrated with Azure databases, storages, queues, event grid, Outlook, SharePoint lists, social media applications such as Twitter and Facebook, or else with a function app. The Logic App dashboard has a workflow run history; it’s beneficial for troubleshooting since you can navigate to each run and examine the data passed in a given step.

    Event Grid

    You can use the Azure event grid for event-based applications. You can publish an event to the event grid and consume it from any platform. It is based on the publisher-subscriber architecture. You get the publishing and subscribing benefit without the overhead of setting up the necessary infrastructure. The event grid can support events from blob storage or any other azure resource. Event grid offers event routing and real-time event delivery at a larger scale. It supports millions of events and it integrates with Azure services as well as other on-prem services outside Azure. The event grid can trigger an Azure function or a logic app and use a push mechanism to send messages when an event occurs. The push mechanism consumes fewer resources and scales better than the polling mechanism. If we use polling, it must check for updates on a regular interval which consumes a lot of resources.

    Before it is serverless

    When it comes to hosting an application, we have passed several paradigms. In this section, let’s try to see what are the pros and cons between those stages and why applications are hosted in a serverless architecture.

    You can find the stages that we have come from on-prem deployment to serverless code:

    Applications hosted in on-prem

    IaaS

    PaaS

    Serverless applications

    Applications in on-prem

    Before our applications were deployed into the cloud, people were interested in hosting their applications in a physical on-premise server.

    We had to think about the following factors when hosting the application in an on-prem physical server:

    What is the hardware that needs to be installed?

    How to provide secure access to the application?

    Do we have to use an uninterrupted power supply to the data center via UPS?

    How can we store storage backups?

    How to provide redundant power to the physical server?

    How to scale the server resources based on the demand when the application is growing?

    How to achieve a highly available system when something goes wrong in the central server?

    Over-allocated servers due to disaster recovery or to store backups.

    We had to manually manage the storage and the memory as per ongoing needs.

    There were so many things to consider and to decide based on the above criteria, so people wanted to find a less complicated and more manageable solution.

    IaaS

    Then the world moved into virtualization technologies such as Hyper-V with virtual machines or VM. This opened up a new platform called Infrastructure as a service (IaaS). IaaS has laid a standard set of servers and a flexible environment to scale or expand based on on-demand needs. In this architecture, the company purchased virtual machines as a service from cloud vendors. We don’t have to worry about the high availability, scaling, or physical machines. Instead of that, we can focus on the virtual environment.

    But IaaS was not enough to satisfy the business requirements; we find the following problems in IaaS architecture:

    Patches and backups in servers

    Installing packages

    Operating system updates

    Monitoring the application

    PaaS

    People tend to find a more natural solution than IaaS to host their applications. So they invented the PaaS architecture. In PaaS, the cloud provider handles the operating system, security patches, and required packages to support a specific platform. We can simply select a platform target such as a web application or API endpoint and deploy the code directly to the environment. In IaaS, we have to install the .NET framework and then set up the IIS servers. But in PaaS, we don’t have to worry about the framework libraries or server environment.

    Still, there are some concerns about PaaS.

    What are the services I need to host my application? - You can only control what has been built in the platform.

    How to scale in or scale out the application? - By adding more servers or increasing the capacity

    Serverless applications

    The serverless architecture will focus on a microservice that does one thing instead of a platform. Serverless applications are event-driven and usually a small chunk of code.

    The serverless architecture will focus on a microservice-based environment. In a microservice architecture, it focuses on doing one thing which is executing a specific method in an isolated environment.

    We have to think of these two design considerations while creating a serverless application:

    What triggers the code?

    What does the code do?

    In a serverless platform, we don’t have to worry about the infrastructure or the host at all; and the host can be an instance of IIS, Kestrel, or Apache or some other web server. What we have to focus on is the web request or the trigger.

    If we select Paas or IaaS, we have to pay for a continuous running application which means that we have to pay for the host when that is not being accessed. But a serverless code emphasizes on micro billing. You have to pay only for the usage; there is no cost when the service is not being used or not running. You can scale the individual function app separately when the usage increases. But vendor lock-inn is one disadvantage in a serverless application. You can create a serverless application with Microsoft Azure functions, Amazon’s Lambda platform or else, Google cloud functions. Since all the infrastructure for the serverless application is provided by the selected vendor, the application is tightly coupled to the vendor.

    First steps with Azure function

    We talked about serverless architecture and its features in the previous chapter. Let’s focus on Azure Functions and features of it in this chapter.

    This chapter is going to cover the azure service integrations that can be used with a function. Then it talks about the languages we can use to develop a function app. At the end of this section, it talks about triggers and bindings that support a function in different runtime versions.

    What is an Azure function?

    The Azure function is a serverless compute service that runs a chunk of code or a small piece of code on-demand without having to provision or manage the infrastructure. We can use the Azure function to run in response to a variety of events in the cloud. Functions make development productive and we can use a developmental language from a variety of choices such as C#, F#, JavaScript, Java, Python, and PHP. You can pay only for the time that your function code runs.

    Features of Azure functions

    We talked about what a Azure function is in the previous section of this chapter. Let’s go through some essential features in Azure functions. After reading these, you will be able to decide whether to go for a function hosted in Azure.

    Function development is easy

    We can implement our function from the Azure portal - Azure CLI, or else by using an Integrated Development Environment (IDE) Visual Studio or Visual Studio Code.

    You have a variety of languages to select from

    You can select a developmental language to start building your function from such as C#, F#, JavaScript, and Java or else, experimental languages such as TypeScript, Python, PHP, Batch, Bash, and PowerShell.

    Set up continuous integration and deployment

    We can set up CI/CD pipeline to deploy your function from GitHub using Azure Cloud shell with Kudu builds or else, Azure DevOps builds.

    You can develop your functions from a Linux environment

    If you love Linux, you can use a Linux environment to develop and deploy your functions.

    Pay for what you use

    In Azure function, you only have to pay for the time spent on running the code. You have two pricing plans to select from: consumption plan and the app service plan.

    Use your dependencies

    You can use Nuget and NPM so that you can use your dependencies inside a function.

    Integrate with other Azure services

    You can integrate your function with other Azure services such as Azure Cosmos DB, Azure Event Hubs, Azure Event Grid, Azure Notification Hubs, Azure Service Bus, Azure Storage (Blobs, Queues or Tables), Azure Logic Apps, Azure SignalR, Microsoft Graph, and Power BI.

    Integrate functions with third-party services

    You can integrate Azure function with third-party services such as Twilio API, SendGrid, and other on-prem

    Enjoying the preview?
    Page 1 of 1