An Azure Functions QuickStart project that demonstrates how to use a Cosmos DB Trigger with Azure Developer CLI (azd) for quick and easy deployment, using Python 3.11 or higher.
This architecture shows how the Azure Function is triggered automatically when documents are created or modified in Cosmos DB through the change feed mechanism. The key components include:
- Client Applications: Create or update documents in Cosmos DB
- Azure Cosmos DB: Stores documents and provides change feed capabilities
- Change Feed: Detects modifications to documents in Cosmos DB
- Azure Function with Cosmos DB Trigger: Executes automatically when changes are detected
- Lease Container: Tracks which changes have been processed to ensure reliability and support for multiple function instances
- Azure Monitor: Provides logging and metrics for the function execution
- Downstream Services: Optional integration with other services that receive processed data
This serverless architecture enables highly scalable, event-driven processing with built-in resiliency.
- Real-time Data Processing Pipeline: Automatically process data as it's created or modified in your Cosmos DB. Perfect for scenarios where you need to enrich documents, update analytics, or trigger notifications when new data arrives without polling.
- Event-Driven Microservices: Build event-driven architectures where changes to your Cosmos DB documents automatically trigger downstream business logic. Ideal for order processing systems, inventory management, or content moderation workflows.
- Cosmos DB Trigger
- Azure Functions Flex Consumption plan
- Azure Developer CLI (azd) integration for easy deployment
- Infrastructure as Code using Bicep templates
- Python 3.11+ support
- Python 3.11+
- Azure Functions Core Tools
- Azure Developer CLI (azd)
- Azurite
- An Azure subscription
-
Clone this repository
git clone https://github.com/Azure-Samples/functions-quickstart-python-azd-cosmosdb.git cd functions-quickstart-python-azd-cosmosdb -
Make scripts executable (Mac/Linux):
chmod +x ./infra/scripts/*.shOn Windows:
set-executionpolicy remotesigned -
Provision Azure resources using azd
azd provision
This will create all necessary Azure resources including:
- Azure Cosmos DB account
- Azure Function App
- App Service Plan
- Other supporting resources
local.settings.jsonfor local development with Azure Functions Core Tools, which should look like this:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "python", "COSMOS_CONNECTION__accountEndpoint": "https://{accountName}.documents.azure.com:443/", "COSMOS_DATABASE_NAME": "documents-db", "COSMOS_CONTAINER_NAME": "documents" } }The
azdcommand automatically sets up the required connection strings and application settings. -
(Optional) Create and activate a Python virtual environment:
python3 -m venv .venv source .venv/bin/activate # On Windows use: .venv\Scripts\activate
-
Install Python dependencies:
pip install -r requirements.txt
-
Start the function locally
func start
Or use VS Code to run the project with the built-in Azure Functions extension by pressing F5.
-
Test the function locally by creating a document in your Cosmos DB container
You can use Azure Portal or Azure CLI to create a document like this:
{ "id": "doc-001", "Text": "This is a sample document", "Number": 42, "Boolean": true }When the document is created or modified, the function will trigger automatically. You should see console output like:
Documents modified: 1 First document: { ... } First document id: doc-001 -
Deploy to Azure
azd up
This will build your function app and deploy it to Azure. The deployment process:
- Checks for any bicep changes using
azd provision - Packages the Python project
- Publishes the function app using
azd deploy - Updates application settings in Azure
Note: If you deploy with
vnetEnabled=true, see the Networking and VNet Integration section below for important details about accessing Cosmos DB and Data Explorer from your developer machine. - Checks for any bicep changes using
-
Test the deployed function by adding another document to your Cosmos DB container through the Azure Portal:
- Navigate to your Cosmos DB account in the Azure Portal
- Go to Data Explorer
- Find your database and container
- Create a new document with similar structure to the test document above
- Check your function logs in the Azure Portal to verify the trigger worked
This function is triggered by changes in Cosmos DB documents using the change feed. The key environment variables that configure its behavior are:
COSMOS_CONNECTION__accountEndpoint: The Cosmos DB account endpointCOSMOS_DATABASE_NAME: The name of the database to monitorCOSMOS_CONTAINER_NAME: The name of the container to monitor
These are automatically set up by azd during deployment for both local and cloud environments.
Here is the core implementation of the Cosmos DB trigger function in this repo:
import os
import azure.functions as func
import logging
app = func.FunctionApp()
@app.cosmos_db_trigger(
arg_name="documents",
container_name=os.environ.get("COSMOS_CONTAINER_NAME"),
database_name=os.environ.get("COSMOS_DATABASE_NAME"),
connection="COSMOS_CONNECTION",
create_lease_container_if_not_exists="true",
)
def cosmos_trigger(documents: func.DocumentList):
logging.info("Python CosmosDB triggered.")
logging.info(f"Documents modified: {len(documents)}")
if documents:
for doc in documents:
logging.info(f"First document: {doc.to_json()}")
logging.info(f"First document id: {doc.get('id')}")
else:
logging.info("No documents found.")The function uses a lease container to track processed changes and support multiple instances. When documents are added or modified in the monitored container, the change feed automatically triggers this function.
You can monitor your function in the Azure Portal:
- Navigate to your function app in the Azure Portal
- Select "Functions" from the left menu
- Click on your function (cosmos_trigger)
- Select "Monitor" to view execution logs
Use the "Live Metrics" feature to see real-time information when testing.
If you deploy with vnetEnabled=true, all access to Cosmos DB is restricted to the private endpoint and the connected virtual network. This enhances security by blocking public access to your database.
Important: When vnetEnabled=true, it is a requirement to add your developer machine's public IP address to the Cosmos DB account's networking firewall allow list. The deployment scripts included in this template run as a part of azd provision and handle this for you. Alternatively it can be done in the Azure Portal or Azure CLI.
