Serverless event-based architectures with Azure Cosmos DB and Azure Functions
Azure Functions provides the simplest way to connect to the change feed. You can create small reactive Azure Functions that will be automatically triggered on each new event in your Azure Cosmos container's change feed.
With the Azure Cosmos DB Trigger, you can leverage the Change Feed Processor's scaling and reliable event detection functionality without the need to maintain any worker infrastructure. Just focus on your Azure Function's logic without worrying about the rest of the event-sourcing pipeline. You can even mix the Trigger with any other Azure Functions bindings.
Currently, the Azure Cosmos DB trigger is supported for use with the Core (SQL) API only.
To implement a serverless event-based flow, you need:
- The monitored container: The monitored container is the Azure Cosmos container being monitored, and it stores the data from which the change feed is generated. Any inserts and changes (e.g., CRUD) to the monitored container are reflected in the change feed of the container.
- The lease container: The lease container maintains state across multiple and dynamic serverless Azure Function instances and enables dynamic scaling. This lease container can be manually or automatically created by the Azure Cosmos DB Trigger.To automatically create the lease container, set the CreateLeaseCollectionIfNotExists flag in the configuration. Partitioned lease containers are required to have a
/idpartition key definition.
Create your Azure Cosmos DB Trigger
Creating your Azure Function with an Azure Cosmos DB Trigger is now supported across all Azure Functions IDE and CLI integrations:
- Visual Studio Extension for Visual Studio users.
- Visual Studio Core Extension for Visual Studio Code users.
- And finally Core CLI tooling for a cross-platform IDE agnostic experience.
Run your Azure Cosmos DB Trigger locally
If you want to test live scenarios in the cloud, you can Try Cosmos DB for free without any credit card or Azure subscription required.
You can now continue to learn more about change feed in the following articles: