Gordon  Matlala

Gordon Matlala

1626081600

How to Create a Serverless API Using Azure Cosmos DB

Azure Cosmos DB is a globally distributed, multi-model, NoSQL database service that allows us to build highly available and scalable applications. Cosmos DB supports applications that use Document model data through its SQL API and MongoDB API.

I’ve meant to produce more content on Cosmos DB’s Mongo API, so in this article, I will be developing a Serverless API in Azure Functions that uses a Cosmos DB MongoDB API account. This article has been loosely based on this fantastic tutorial on creating a Web API with ASP.NET Core and MongoDB.

By the end of this article, you’ll know how to create a Cosmos DB account that uses the MongoDB API. You’ll also know how to create a simple CRUD Web API in C## that interacts with a Mongo DB API account.

If you want to see the whole code before diving into the article, you can check it out on my GitHub here.

What you’ll need to follow along with this article:

  • Visual Studio 2019 with the Azure Development workload.
  • Azure Subscription.
  • Postman.

#How does Azure Cosmos DB support a MongoDB API?

Azure Cosmos DB implements the wire protocol for MongoDB, allowing us to use client drivers and tools that we’re used to, but allow us to host our data in Azure Cosmos DB.

This is great if we already have applications that use MongoDB. We can change our applications to use Azure Cosmos DB without making significant changes to our codebase. We can also benefit from Azure Cosmos DB, such as Turnkey distribution and elastic scalability in both throughput and storage.

#Setting up an Account with the MongoDB API

Let’s set up our Cosmos DB account. Login to Azure and click on Create a resource. Look for Azure Cosmos DB and click ‘New’.

On the ‘Create Azure Cosmos DB Account’ page, provide the following configuration:

  • Resource Group — Resource groups in Azure are a logical collection of resources. For this tutorial, you can create a new one or add your account to an existing one.
  • Account Name — This will be the name of your account. It needs to be unique across Azure, so make it a good one :) API — Azure Cosmos DB is a multi-model database, but when we create a Cosmos DB account, we can only pick one API, and that will be the API for the lifetime of the account. Since this is a Mongo DB API tutorial, pick the ‘Azure Cosmos DB for MongoDB API’ option.
  • Location — Where we want to provision the account. I’ve chosen Australia East as that’s the data center close to me, but pick a data center close to you.
  • Capacity Mode — In a nutshell, this is how throughput will be provisioned in this account. I have written articles on how throughput works in Cosmos DB if you are interested, but for now, choose ‘Serverless’ (still in preview at the time of writing).
  • Account Type — Choose production
  • Version — This is the version of the MongoDB wire protocol that the account will support. Choose 3.6
  • Availability Zones — Disable this for now.

#azure #csharp #mongodb #serverless

How to Create a Serverless API Using Azure Cosmos DB