As serverless deployment is becoming popular, equipping yourself with some serverless skills will be paramount. In this article, you will learn how to build a CRUD API using Azure Functions and Azure Cosmos DB 🤩.

Why Go Serverless with Azure Functions

From Microsoft’s site, Azure Functions is a serverless compute service (Function as a service) provided by Azure, Microsoft’s cloud service. It lets you run event-triggered code without having to provision or manage any infrastructure explicitly. It contrasts with the classic approach that requires setting up your server, maintaining it, and taking full responsibility for securing it.

Hosting your code on Azure Functions provides you with some cool benefits:

  • Cost-effective: Using Azure Functions is probably cheaper for a medium-sized project than running your back-end logic on a dedicated server. In fact, after a function executes, it stops consuming resources, and you are only billed for the number of resources used. Azure Functions only run when triggered by an event. Various services can trigger an Azure Function to run, such as an HTTP request, a Timer, an Azure Blob storage upload, etc.
  • Focus on app logic: Since Azure handles the work of provisioning or maintaining a server, you can dedicate your time more to developing the application logic. This boosts productivity.

Setting up Azure Functions

Let’s take a concrete look at how Azure Functions work by building a CRUD API to manage a wishlist.

As a first step, go to the Azure portal.

You need an active Azure subscription to use the cloud services on Azure, including Azure Functions.

It is good practice to wrap all your resources inside a resource group. This makes deleting all used resources inside a resource group easy. So click on Resource groups icon on your Azure dashboard, as shown below.

Next, click on the Add button in the Resource groups page to create a resource group and wait for the Create a resource group page to load.

In the Resource group input field, you can the name for your resource group. In this tutorial, I will use crudtutorial as the resource group’s name. You can also select any region closer to where you live from the Region field. I will leave that field as it is and click on the Review + create button.

After your settings have been reviewed and validated, the Review + create button will change to Create. Finally, click on the Create button to create your resource group.

After the resource group has been created, you will be redirected back to the Resource group page. Click on the crudtutorial resource group or whatever name you chose for your resource group.

The crudtutorial resource group page will open. Click on the Add button to add a resource to the resource group, as shown in the following image:

You will be redirected to a search page for selecting the resource to be created. You can either search for Function App in the search field (a) or select Function App from the list of popular resources (b).

The Create Function App page opens. Configure the following settings, as depicted by the image below:

  1. Assign a unique name to your Function App. I’m using swacblooms in the Function App example I’m building. This is also the name that will be prepended to .azurewebsites.net to form the Function App domain name (e.g., swacblooms.azurewebsites.net).
  2. Select Node.js as the Runtime stack since the function logic will be written in JavaScript.
  3. Choose 12 LTS or any other version as the Node.js version.
  4. Select any region of your choice, preferably a region closer to where you are.
  5. Finally, click on the Review + create button, wait for validation, and then continue by clicking on the Create button.

Wait for the required resources for your Function App are provisioned. When the Go to resource button becomes active, click on it to navigate to the newly created Function App dashboard.

The overview page will display general information related to your Function App.

Now, you can focus on creating the CRUD API for your wishlist. So, click on the Functions menu on the left panel.

The CRUD API will be implemented by seven functions:

  • initialize-list: For generating a sample wishlist in the database.
  • get-list: For retrieving all the wishlist items from the database.
  • get-a-list-item: For retrieving a specific wishlist item from the database.
  • delete-list-items: For deleting all the wishlist items in the database.
  • delete-a-list-item: For deleting a wishlist item from the database.
  • create-a-list-item: For inserting a new wishlist item to the database.
  • update-a-list-item: For updating a wishlist item in the database.

To start, click on the Add button to create a function. A right panel will slide in containing the required fields needed to configure the function, as you can see in the following image.

On the right panel, you see various available templates to bootstrap your function. Since this tutorial is centered on making a CRUD API triggered through an HTTP request, select HTTP trigger as the template to use.

Scroll down a little bit on the right panel, and you will see the New Function field, which allows you to provide the function’s name.

  1. Clear the default function name and replace it with the name of the first function to create, i.e., initialize-list.
  2. In the Authorization level field, select Anonymous. This setting will allow you to call the function without attaching an authorization token to the request.
  3. Finally, click on the Add button.

#crud api #azure #azure functions #azure cosmos db

Making a CRUD API using Azure Functions and Azure Cosmos DB
2.40 GEEK