Separating the database from the compute. Creating an Azure Cosmos DB Serverless Instance
SR

Steve Rich

Created Jun 25, 2025

Separating the database from the compute. Creating an Azure Cosmos DB Serverless Instance

In this lab we will continue to use a virtual machine for the compute, but we'll use Azure Cosmos DB for the (noSQL) database. As you'll see later there are various configurations we must do, that are needed for the Python installation. This will give you an appreciation of why many people choose to use containers!

0
    p

Go to the Azure Portal

19 Steps
1

Click on Create a resource

We'll create the noSQL database first. We'll use Cosmos DB, Microsoft's managed noSQL service

Click on Create a resource
2

Type "cosmos"

Type "cosmos"
3

Click on Create

Click on Create
4

Click on Azure Cosmos DB

Click on Azure Cosmos DB
5

Click on Create

Click on Create
6

Click on Workload Type

Click on Workload Type
7

Click on Learning

This will be perfectly adequate for our needs. We could select Development/Training, but it would likely mean that we were paying for something we weren't fully utilising.

Click on Learning
8

Click on Create new

I chose to create a new resource group to keep my Cosmos DB in.

Click on Create new
9

Give it a sensible name

I chose: "ICC1tutorial2"

Give it a sensible name
10

Give your database instance a name.

I called mine "icc-cosmo", but if I had a lot of resources, that might not be the best name.

Give your database instance a name.
11

Change your location to one that's suitable

Change your location to one that's suitable
12

Click on Serverless…

Based on our workload for this lab, I think this is the most appropriate.

Note underneath as well that we'll likely stay within the free tier of Azure Cosmos.

Click on Serverless…
13

Click on Review + create

Click on Review + create
14

It should pass Validation. Now click on Create

It should pass Validation. Now click on Create
15

You'll see a message that 'Deployment is in progress'

You'll see a message that 'Deployment is in progress'
16

Once it's successfully deployed: Click on Go to resource

Once it's successfully deployed: Click on Go to resource
17

Now there's not much for us to look at so far

We could use the Data Explorer to.... explore our data, but we don't have any yet!

Now there's not much for us to look at so far
18

Click on Keys

What we do need though are the access keys that we'll use in our code, that will allow our application (running on a VM) to communicate with our Cosmos DB.

Click on Keys
19

You are going to need the URI and the Primary Key

Click on the Eye šŸ‘ at the right hand side to show each, and then take a copy of each. (Paste it somewhere you can easily get to, like Notepad for example.)

You are going to need the URI and the Primary Key

#

Part 2 - Setting up the VM and code

1 Step

Now I'm not going to show you how to set up a virtual machine for this part. you could refer to the first lab or you could do what I did and use the same virtual machine that I used in the first lab and use it for the 2nd lab.

This part of the tutorial is the most complicated due in part to the setting up that we have to do in this next section.

20

Get in to your Cloud Shell for the Virtual Machine you are going to use.

You can see here that I'm using the same virtual machine as I used in the first lab

Get in to your Cloud Shell for the Virtual Machine you are going to use.
    g

GitHub

21 Steps

Let's take a copy or clone of the code that we need for this virtual machine. This is still the To Do app but the code has been modified to make use of your Cosmos DB instead

21

Click on Code

Click on Code
22

Click on HTTPS

Click on HTTPS
23

Click on Copy url to clipboard

Click on Copy url to clipboard
24

Back at the Cloud Shell. Enter: git clone ..

Back at the Cloud Shell. Enter: git clone ..
25

Confirm the code has been cloned on to your VM.

You can see here that the clone successfully happened and then I used the cd command to change directory into the ICC1_todoapp_2 folder. I then used the ls command to list all of the files in that folder.

Confirm the code has been cloned on to your VM.
26

Python and all the libraries can get confusing

Plus, we don't want to install libraries that we don't need-system wide.

So we need to install Python's 'virtual environment' options.

sudo apt install python3.12-venv

Python and all the libraries can get confusing
27

Now it's installed, we can create a virtual environment

python3 -m venv venv

Now it's installed, we can create a virtual environment
28

Then we can activate the virtual environment

source venv/bin/activate

Then we can activate the virtual environment
29

Now your prompt indicates you are using the virtual environment

Now your prompt indicates you are using the virtual environment
30

Now we can install the libraries that we need. Flask and Cosmos DB. Run each of these three commands:

sudo apt install python3-pip

pip install flask

pip install azure-cosmos

31

Now we need to set our environment variables so that the code will connect with our Cosmos DB. We use environment variables to ensure that our connection details are kept private.

You can see in the image below I've typed export COSMOS_ENDPOINT="my URI here" In between the quotes, paste in the URI saved earlier. Remember your URI will be different to mine!

Now we need to set our environment variables so that the code will connect with our Cosmos DB.  We use environment variables to ensure that our connection details are kept private.
32

Then we do the same with the COSMOS_KEY

export COSMOS_KEY="your key here"

Then we do the same with the COSMOS_KEY
33

We need to tweak the network settings for our VMClick on Network Settings, then Security

This version of the app uses port 8080

We need to tweak the network settings for our VM
Click on Network Settings, then Security
34

Click on Create port rule

Click on Create port rule
35

Click on Inbound port rule

Click on Inbound port rule
36

Click on TCP

Click on TCP
37

Click on Add

Click on Add
38

Now let's try the app! Start your app with:

python3 app.py (You can see me doing this in the above screenshot).

39

Go to the IP address of your VM, with port 8080 on the end

Go to the IP address of your VM, with port 8080 on the end
40

It should work!

Add some tasks

It should work!
41

Congratulations!

If you flick back to the Azure Portal and go to your Cosmos DB - and go to Data Explorer, you should be able to see the names of the tasks there, proving they are being stored in the database.

Congratulations!
Well done!
Create how-to guides like this in a snap. Get Tango now.