Duration: 45 minutes | Day: 2
In this lesson, you'll work with Azure Cosmos DB, a globally distributed NoSQL database. By the end, you'll be able to:
- Create a Cosmos DB account and understand serverless vs provisioned pricing
- Store and query JSON documents using Data Explorer
- Use partition keys for efficient data distribution
- Perform CRUD operations with the Python SDK
| Challenge | How Cosmos DB Solves It |
|---|---|
| Need flexible schema | Store any JSON structure - no migrations needed |
| Global users | Automatic multi-region replication |
| Unpredictable traffic | Serverless mode scales to zero and auto-scales up |
| Different data models | One service supports documents, graphs, key-value, tables |
Real-world example: E-commerce product catalogs, user profiles, IoT telemetry, gaming leaderboards - anywhere you need flexible, scalable data storage.
┌─────────────────────────────────────────────────────────────────┐
│ Lesson 09 Workflow │
├─────────────────────────────────────────────────────────────────┤
│ 1. PORTAL: Create Cosmos DB account (5 min) │
│ 2. PORTAL: Create database and container (3 min) │
│ 3. PORTAL: Add data using Data Explorer (5 min) │
│ 4. TERMINAL: Run Python app to test CRUD operations (10 min) │
│ 5. PORTAL: Query data with SQL syntax (5 min) │
└─────────────────────────────────────────────────────────────────┘
- Completed infrastructure deployment (
azd up) - Azure Portal access
-
Go to Azure Portal
-
Search "Cosmos DB" → Click Create
-
Select Azure Cosmos DB for NoSQL → Create
-
Fill in:
Setting Value Subscription Your subscription Resource Group rg-azure-essentials-dev(or create new)Account Name cosmos-yourname-dev(must be globally unique)Location Same as other resources Capacity mode Serverless (pay per request - best for learning) -
Click Review + create → Create
-
Wait for deployment (~3 minutes)
-
Go to your Cosmos DB account
-
Click Data Explorer in the left menu
-
Click New Container
-
Fill in:
Setting Value Database id azure-essentials(check "Create new")Container id itemsPartition key /category -
Click OK
- In Data Explorer, expand azure-essentials → items
- Click Items → New Item
- Replace the JSON with:
{
"id": "1",
"category": "electronics",
"name": "Laptop",
"description": "High-performance laptop",
"price": 999.99
}- Click Save
- Add a second item:
{
"id": "2",
"category": "electronics",
"name": "Phone",
"description": "Smartphone with 5G",
"price": 799.99
}- Add a third item in a different category:
{
"id": "3",
"category": "books",
"name": "Azure Guide",
"description": "Learn Azure fundamentals",
"price": 49.99
}- In Data Explorer, click New SQL Query
- Try these queries:
-- Get all items
SELECT * FROM c
-- Get items by category (uses partition key - efficient!)
SELECT * FROM c WHERE c.category = "electronics"
-- Get item names and prices
SELECT c.name, c.price FROM c
-- Filter by price
SELECT * FROM c WHERE c.price > 100- Click Execute Query to run each one
Tip: Use Azure Cloud Shell - it has Python and Azure CLI pre-installed, and you're already logged in!
- Click the Cloud Shell icon in the Azure Portal (top right:
>_) - Select Bash when prompted
- Run these commands:
# 1. Auto-discover your Cosmos DB account
COSMOS_ACCOUNT=$(az cosmosdb list --query "[0].name" -o tsv)
RG=$(az cosmosdb list --query "[0].resourceGroup" -o tsv)
echo "Found: $COSMOS_ACCOUNT in $RG"
# 2. Get credentials
export COSMOS_ENDPOINT=$(az cosmosdb show --name $COSMOS_ACCOUNT --resource-group $RG --query documentEndpoint -o tsv)
export COSMOS_KEY=$(az cosmosdb keys list --name $COSMOS_ACCOUNT --resource-group $RG --query primaryMasterKey -o tsv)
# 3. Clone repo and run the test app
git clone https://github.com/codetocloudorg/azure_essentials.git 2>/dev/null || true
cd azure_essentials/lessons/09-database-services/src/cosmos-test-app
# 4. Install dependencies and run
pip install --user -r requirements.txt
python app.pyLocal Bash/Zsh: The same commands work on macOS/Linux if you have Azure CLI installed.
# 1. Auto-discover your Cosmos DB account
$cosmosAccount = (az cosmosdb list --query "[0].name" -o tsv)
$rg = (az cosmosdb list --query "[0].resourceGroup" -o tsv)
Write-Host "Found: $cosmosAccount in $rg"
# 2. Get credentials
$env:COSMOS_ENDPOINT = (az cosmosdb show --name $cosmosAccount --resource-group $rg --query documentEndpoint -o tsv)
$env:COSMOS_KEY = (az cosmosdb keys list --name $cosmosAccount --resource-group $rg --query primaryMasterKey -o tsv)
# 3. Clone repo and run the test app
git clone https://github.com/codetocloudorg/azure_essentials.git 2>$null
cd azure_essentials/lessons/09-database-services/src/cosmos-test-app
# 4. Install dependencies and run
pip install --user -r requirements.txt
python app.pyExpected Output:
==================================================
Cosmos DB CRUD Operations Demo
==================================================
1. Creating items...
Created item: abc123-...
2. Reading items...
Found 2 items in category 'electronics'
- Laptop: High-performance laptop
- Phone: Smartphone with 5G
3. Updating item...
Updated item: abc123-...
4. Verifying update...
Updated description: Updated: High-performance gaming laptop
New price: $1299.99
5. Deleting item...
Deleted item: def456-...
==================================================
Demo complete!
==================================================
- Go back to Data Explorer in Azure Portal
- Click Refresh on the container
- You should see new items created by the Python app
- Run a query to see all items:
SELECT * FROM c ORDER BY c.createdAt DESCThe Python app in src/cosmos-test-app/app.py demonstrates CRUD operations:
| Operation | Method | Description |
|---|---|---|
| Create | container.create_item() |
Adds a new JSON document |
| Read | container.query_items() |
Queries with SQL syntax |
| Update | container.replace_item() |
Replaces entire document |
| Delete | container.delete_item() |
Removes document by id + partition key |
Connect to Cosmos DB:
from azure.cosmos import CosmosClient
client = CosmosClient(ENDPOINT, KEY)
database = client.get_database_client("azure-essentials")
container = database.get_container_client("items")Create an item:
item = {
"id": str(uuid.uuid4()), # Unique ID (required)
"category": "electronics", # Partition key (required)
"name": "Laptop",
"price": 999.99
}
container.create_item(body=item)Query items (using partition key for efficiency):
query = "SELECT * FROM c WHERE c.category = @category"
items = container.query_items(
query=query,
parameters=[{"name": "@category", "value": "electronics"}]
)| Concept | Description |
|---|---|
| Account | Top-level resource, globally unique name |
| Database | Logical container for multiple containers |
| Container | Where your data lives (like a table) |
| Partition Key | Property used to distribute data (e.g., /category) |
| Item | A single JSON document |
| Mode | Best For | Billing |
|---|---|---|
| Serverless | Development, learning, spiky workloads | Pay per request |
| Provisioned | Production, predictable workloads | Reserved throughput |
| Level | Guarantee | Use Case |
|---|---|---|
| Strong | Always read latest | Financial transactions |
| Session | Read your own writes | Most applications (default) |
| Eventual | May read stale data | Analytics, metrics |
Use --user flag or a virtual environment:
# Option 1: Install for current user only
pip install --user -r requirements.txt
# Option 2: Use a virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/macOS
# .venv\Scripts\activate # Windows
pip install -r requirements.txtBash/Zsh:
# Verify credentials are set
echo $COSMOS_ENDPOINT
echo $COSMOS_KEYPowerShell:
$env:COSMOS_ENDPOINT
$env:COSMOS_KEYThe app expects:
- Database:
azure-essentials - Container:
items - Partition key:
/category
Create them in Data Explorer if they don't exist.
Serverless has request limits. Wait a moment and retry.
Delete the Cosmos DB account when done:
Bash/Zsh:
az cosmosdb delete --name $COSMOS_ACCOUNT --resource-group $RG --yesPowerShell:
az cosmosdb delete --name $cosmosAccount --resource-group $rg --yes| Task | Completed |
|---|---|
| Created Cosmos DB account (Serverless) | ✅ |
| Created database and container | ✅ |
| Added data via Portal | ✅ |
| Queried data with SQL | ✅ |
| Tested CRUD with Python app | ✅ |
Continue to Lesson 10: Billing and Cost Optimisation