Home Programming Binding Service Bus to Azure CosmosDB change feed.

Binding Service Bus to Azure CosmosDB change feed.


In this article, we will learn how to listen to change feeds in Azure function and then send it to the service bus. Let’s start with the basic concept.

What is Azure?

Microsoft Azure is a cloud computing service created for building, testing, deploying, and managing applications and services through Microsoft-managed data centers.

What is ComosDB?

Microsoft CosmosDB is a fully managed NoSQL database service which is globally distributed.

What is Service Bus?

Microsoft Azure Service Bus is a fully managed enterprise integration message broker. Service Bus offers a reliable and secure platform for asynchronous transfer of data between different applications.

What is Azure Function?

Azure Functions allows you to run small pieces of code (called “functions”) without worrying about application infrastructure. You can write your code in languages supported by it. Just write and run it.

What is Change Feed?

Change feed support in Azure Cosmos DB works by listening to an Azure Cosmos container for any changes.

Step 1:

Create a database and collection in Azure cosmos db.

Step 2:

  • Create a new function app and follow the below steps:
    • Click on Add and then select Azure Cosmos DB trigger from the template.
    • Fill all the required details.
    • Click on Create Function.
  • Select the function you have created.
  • Click on Code + test.
  • You will see a drop-down with two files run.csx and function.json

Step 3:

Copy the below code in respective files


#r "Microsoft.Azure.DocumentDB.Core"
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;

public static void run(IReadOnlyList<Document> input, ILogger log,out Model outputSbMsg)
    if (input != null && input.Count > 0)
	Model model;
        log.LogInformation("Documents modified " + input.Count);
        foreach (Document doc in input) 
            log.LogInformation("Document Id " + doc.Id);
            log.LogInformation("Base Model Id " + doc.GetPropertyValue<string>("baseModelId"));
	    // Initializing the custom class that we need to need on queue.
	    model=new Model();
	    // Assiging the class object to the queue object	
            outputSbMsg = model; 

// Custom class
public class Model{
    public string id { get; set; }
    public string baseModelId { get; set; }


  "bindings": [
      "type": "cosmosDBTrigger",
      "name": "input",
      "direction": "in",
      "connectionStringSetting": "cosmos-db_DOCUMENTDB",
      "databaseName": "Product",
      "collectionName": "model",
      "leaseCollectionName": "leases",
      "createLeaseCollectionIfNotExists": true
      "connection": "poc2020_RootManageSharedAccessKey_SERVICEBUS",
      "name": "outputSbMsg",
      "queueName": "outqueue",
      "direction": "out",
      "type": "serviceBus"

IReadOnlyList<Document> input: Contains the list of documents that are modified(created/changed).

out Model outputSbMsg: This is the service bus variable. The data in this variable will be pushed to the queue. In the code, we have assigned the model object to this variable because as we need model data in the queue.

Binding of both variables is defined in function.json. These bindings are based on the configuration of the cosmos DB and the service bus.

You can set all this by editing this file or clicking on the integration tab on left hand side menu below code + test.

Step 4: Updating bindings.

Click on Integration tab and you will see below screen.

  • Click on Add output
  • Select Azure Service Bus in binding type.
  • Fill all the details (Service Bus should be configured for this.)

All the configurations are now done. 🙂

You can download the code from GitHub Repo: https://github.com/amitkhurana92/azure-change-feed-comosdb-to-service-bus

If you are facing any problem then do write to us in comments section below.

Happy Coding 🙂

This site uses Akismet to reduce spam. Learn how your comment data is processed.