How to Create a Serverless Cosmos DB
In our lab walkthrough series, we go through selected lab exercises on our INE Platform. Subscribe or sign up for a 7-day, risk-free trial with INE and access this lab and a robust library covering the latest in Cyber Security, Networking, Cloud, and Data Science!
The purpose of this lab is to help you in understanding the creation of Serverless Cosmos db, using both Azure portal and Azure CLI.
Technical Difficulty:
| Novice | Beginner | Proficient | Expert
What is Serverless Cosmos DB?
Firstly, we will understand what is Cosmos DB? It is a database service that is globally distributed. So, it enables its users to manage their data from anywhere in the world. It is a fully managed, serverless NoSQL database for high-performance applications of any size or scale.
It also offers 2 different capacity modes to its users namely the provisioned throughput and the serverless. Whatever you choose you can perform the exact same database operations in both the modes.
In this blog we are going to learn how to use the serverless Cosmos DB. The serverless Azure Cosmos DB account offers the user to use their account in consumption-based fashion. With the serverless, one will be charged for the consumption of storage by your data and the Request Units (RUs) consumed by the database operations.
Objective: Create a CosmoDB (Serverless) NoSQL Database using azure portal and CLI.
Lab: Click here to open the Lab
Start the lab, agree after reading terms and conditions and check the credential/URL provided on the page.
Solution: creating a CosmosDB (Serverless) NoSQL Database using Azure portal
Step 1: Click on the “Azure Portal login” and fill the credentials.
Step 2: After logging into the portal, you will see the Azure portal dashboard along with the empty Resource Group, then click the “+ Create” that is shown in the image.
Step 3: Select the Azure Cosmos DB. Azure Cosmos DB is a serverless NoSQL database for high-performance applications of any size or scale.
Step 4: In “Select API option” go with Core (SQL)
Step 5: Select the existing resource group and enter a unique Account Name. In Capacity mode, select Serverless. In Serverless, there the instance scales as per workload. So fixed capacity is not needed.
Note: The Name of the account should be globally unique else an exception would be raised.
Click on Review + create. If the validation is successful, select create.
The following window will appear on successful deployment then click on “Go to resource”.
Click on the Overview to view more details. Select Add container. A Container is used to hold data items together in Azure.
Step 6: A window will pop-up on the right-hand side of the web browser. Add details of database-Id and container-Id. After filling the details that are shown in the image then directly click on “Ok” button.
Step 7: The database along with the container will be created as below. Select the items option then click on New Item and an item will be visible on the right side of the web browser.
Edit the item and save it. Azure Cosmos item can represent either a document in a collection, a row in a table, or a node or edge in a graph.
{
"id": "first_item",
"name": "first item for demonstration",
"_rid": "-fFcALxcmBABAAAAAAAAAA==",
"_self": "dbs/-fFcAA==/colls/-fFcALxcmBA=/docs/-fFcALxcmBABAAAAAAAAAA==/",
"_etag": "\"210ed147-0000-0700-0000-6284b88f0000\"",
"_attachments": "attachments/",
"_ts": 1652865167
}
Save and Select the Query icon. The following Query window will open up . You can execute your query from here.
Now, we are able to create it successfully.
SELECT * FROM c where c.id = "first_item"
Step 7: Now let’s see how to delete the item in the database using delete option.
Step 8: Delete the container and database using delete option.
A window will pop up on the right side for deletion.
Step 9: Delete the cosmos DB account using delete option. Click on “Overview” to see the “Delete option”
Enter the storage name for confirmation in the following and proceed with deletion.
We have successfully created and deleted using the Portal now it’s the time to work with CLI.
Create a CosmosDB (Serverless) NoSQL Database using Azure CLI:
Note: To work with CLI firstly, you need to install the azure CLI on your machine. If you don’t know how to install then scroll down to check the “Appendix A” on how to install azure CLI on your machine. If you have already installed then follow the steps mentioned below.
Step 1: Open Powershell or cmd (Command Shell) and login into the account. This will open a new windows in the browser where you will to choose the account to sign in.
Execute the command on your favorite CLI az login
az login
Now, run the command “az group list” and note down the both subscription id (right-hand side of the word subscriptions in the 1st highlighted box) and resourceGroup id (right-hand side of the word resourceGroups in the 2nd highlighted box).
az group list
Step 2: Create a Serverless cosmos DB account by EnableServerless
Replace the resource group id with your id in the below code
az cosmosdb create --name inetestdatabase --resource-group replace group id here --default-consistency-level Eventual --locations regionName="East US" failoverPriority=0 isZoneRedundant=False --capabilities EnableServerless
It will take a few minutes. Below is the output for successful deployment.
Step 3: Create a database in the same account
az cosmosdb sql database create --account-name inetestdatabase --resource-group replace group id here --name inedatabase
Step 4: Create a container in the same database
az cosmosdb sql container create --account-name inetestdatabase --resource-group replace group id here --database-name inedatabase --name inetestcontainer --partition-key-path "/partitionKey"
Step 5: Delete the resources
az cosmosdb delete --name inetestdatabase --resource-group replace group id here
Deleting takes time.
Step 6: Switch to a web browser and inspect the Resource Group in Azure Portal. And you can see the resource group is empty.
Conclusion
In this article we saw how to create a CosmosDB(Serverless), NoSQL Database and interact with it using Azure portal and CLI.
Reference:
Azure Cosmos DB(https://azure.microsoft.com/en-us/services/cosmos-db/)
To perform these tasks hands-on in our lab, subscribe or sign up for a 7-day, risk-free trial with INE to access this lab and a robust library covering the latest in Cyber Security, Networking, Cloud, and Data Science!
Appendix A : Install azure cli on Windows and Linux
- For Windows, the Azure CLI is installed via a MSI, which gives you access to the CLI through the Windows Command Prompt (CMD) or PowerShell.
- Download the latest release of Azure CLI from https://aka.ms/installazurecliwindows An MSI file will be downloaded as shown below.
3. Open the msi file and It will prepare to install cli on your local machine. Accept the terms and conditions. Click on install.
4. When the installer asks if it can make changes to your computer, click the “Yes” box. After the installation is complete, you will need to close and reopen any active Windows Command Prompt or PowerShell windows to use the Azure CLI.
5. For the macOS platform, you can install the Azure CLI with Homebrew package Managers which used to manage your CLI installation.
6. Download homebrew from https://docs.brew.sh/Installation.html
7. Install brew by using following command in bash.
brew update && brew install azure-cli
8.For Ubuntu/Debian platform, Installation can be done with one command given below.
This script is downloaded via curl and piped directly to bash to install the CLI.
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
9. You can check if the installation is successful with any azure command.