Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Cloud (AWS/GCP/Azure)intermediate

Cosmos DB Setup

Share

Set up Azure Cosmos DB

Works with OpenClaude

You are an Azure cloud infrastructure engineer. The user wants to set up Azure Cosmos DB with proper configuration, connection, and data initialization.

What to check first

  • Run az account show to verify you're authenticated to the correct Azure subscription
  • Run az cosmosdb list --output table to see if a Cosmos DB account already exists
  • Verify the Azure CLI is installed: az --version

Steps

  1. Create a resource group with az group create --name myResourceGroup --location eastus
  2. Create a Cosmos DB account using az cosmosdb create --resource-group myResourceGroup --name mycosmosdb --kind GlobalDocumentDB (this takes 5-10 minutes)
  3. Get the primary connection string: az cosmosdb keys list --resource-group myResourceGroup --name mycosmosdb --type connection-strings --query connectionStrings[0].connectionString
  4. Create a database within the account using az cosmosdb sql database create --resource-group myResourceGroup --account-name mycosmosdb --name myDatabase
  5. Create a container (collection) with az cosmosdb sql container create --resource-group myResourceGroup --account-name mycosmosdb --database-name myDatabase --name myContainer --partition-key-path "/id"
  6. Set throughput (RUs) for the container: az cosmosdb sql container throughput update --resource-group myResourceGroup --account-name mycosmosdb --database-name myDatabase --name myContainer --throughput 400
  7. Store the connection string as an environment variable for your application to consume
  8. Test connectivity by initializing a client connection and performing a read operation

Code

import os
from azure.cosmos import CosmosClient, PartitionKey

# Retrieve connection string from environment or Azure Key Vault
connection_string = os.getenv("COSMOS_CONNECTION_STRING")
database_name = "myDatabase"
container_name = "myContainer"

# Initialize Cosmos DB client
client = CosmosClient.from_connection_string(connection_string)

# Get or create database
database = client.create_database_if_not_exists(id=database_name)
print(f"Database '{database_name}' ready")

# Get or create container with partition key
container = database.create_container_if_not_exists(
    id=container_name,
    partition_key=PartitionKey(path="/id"),
    offer_throughput=400
)
print(f"Container '{container_name}' ready")

# Insert sample document
sample_item = {
    "id": "item-001",
    "name": "Sample Item",
    "category": "example",
    "price": 29.99
}

created_item = container.create_item(body=sample_item)
print(f"Inserted document: {created_item['id']}")

# Query documents
query = "SELECT * FROM c WHERE c.category = @category"

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
cloudazurecosmos-db

Install command:

curl -o ~/.claude/skills/cosmos-db-setup.md https://claude-skills-hub.vercel.app/skills/cloud/cosmos-db-setup.md

Related Cloud (AWS/GCP/Azure) Skills

Other Claude Code skills in the same category — free to download.

Want a Cloud (AWS/GCP/Azure) skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.