Azure Cosmos DB Management Client Library for Python
The Microsoft Azure Cosmos DB Management Client Library for Python provides capabilities to manage Azure Cosmos DB resources, such as creating, updating, and deleting Cosmos DB accounts, databases, and containers. It interacts with the Azure Cosmos DB resource provider (control plane). The current version is 9.9.0, and it generally follows a regular release cadence as part of the Azure SDK for Python updates.
Warnings
- breaking Version 9.9.0 introduced several breaking changes including removal of parameters from models (e.g., `BackupResource`, `CommandPostBody`, `ServiceResourceCreateUpdateParameters`) and deletion/renaming of client operation groups and enum values. Review release notes carefully when upgrading.
- breaking Older versions (pre ~0.8.0) had module reorganization which changed import paths for `CosmosDB` client and direct access to models and operations submodules. This can cause `ImportError` for legacy code.
- gotcha `DefaultAzureCredential` relies on specific environment variables for non-interactive/service principal authentication (e.g., `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRIPTION_ID`). Incorrect or missing configuration will lead to authentication failures.
- deprecated Support for Python 2.7 officially ended on January 1, 2022. Using `azure-mgmt-cosmosdb` with Python 2.7 is unsupported and will likely lead to issues.
- gotcha Synchronous I/O operations are generally discouraged and can be blocked by default in newer Azure SDKs. While primarily affecting data plane SDKs and .NET EF Core, it's a good practice to use async patterns or ensure long-running management operations are handled with polling for a responsive application.
Install
-
pip install azure-mgmt-cosmosdb azure-identity
Imports
- CosmosDBManagementClient
from azure.mgmt.cosmosdb import CosmosDBManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
- CosmosDB
from azure.mgmt.cosmosdb import CosmosDBManagementClient
- models.*
from azure.mgmt.cosmosdb.models import <ModelName>
- operations.*
from azure.mgmt.cosmosdb.operations import <OperationGroup>
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.cosmosdb import CosmosDBManagementClient
# Set these environment variables for authentication
# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET for service principal
# AZURE_SUBSCRIPTION_ID
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
if subscription_id == "YOUR_SUBSCRIPTION_ID":
print("Please set the AZURE_SUBSCRIPTION_ID environment variable or replace 'YOUR_SUBSCRIPTION_ID' in the code.")
exit()
# Authenticate using DefaultAzureCredential
# This will try to authenticate using various methods (env vars, managed identity, CLI, etc.)
credential = DefaultAzureCredential()
# Create a CosmosDB management client
client = CosmosDBManagementClient(credential, subscription_id)
# Example: List all Cosmos DB accounts in the subscription
print("Listing Cosmos DB accounts...")
for account in client.database_accounts.list():
print(f" - {account.name} (Resource Group: {account.resource_group})")
print("Client initialized successfully and listed accounts.")