TIP

🔥 Help shape the future of Azure Tips and Tricks by telling what you'd like for us to write about here (opens new window).

💡 Learn more : Azure Functions Triggers and Bindings (opens new window).

📺 Watch the video : How to trigger an Azure Function from Azure Cosmos DB (opens new window).

# How to trigger an Azure Function from Azure Cosmos DB

# Azure Functions triggers and bindings

Azure Functions (opens new window) are great for running small workloads, and they can run serverless (opens new window), which means that they scale automatically at a low price. They also offer triggers and bindings (opens new window) for many Azure and third-party services. These enable you to start a Function when an event happens in a service, like when a new file is uploaded to Azure Blob Storage (opens new window). They also help you send output to services, like inserting a row in an Azure SQL Database (opens new window), without having to write any plumbing code to connect to them. Triggers and bindings make Azure Functions easy to use.

In this post, we are going to use the Azure Cosmos DB Trigger (opens new window) to start an Azure Function.

# Prerequisites

If you want to follow along, you'll need the following:

# Use the Azure Cosmos DB Trigger

Let's create an Azure Function and trigger by modifying items in Azure Cosmos DB.

  1. Go to the Azure portal (opens new window)
  2. Click the Create a resource button (the plus-sign in the top left corner)
  3. Search for Functions, select the "Function App" result and click Create
    1. Select a Resource Group
    2. Fill in a Name
    3. Leave Publish to Code
    4. Choose .NET for the Runtime stack
    5. Select the latest version of .NET
    6. Pick a Region for the app
    7. Click Review + create and then Create

(Create an Azure Function App in the Azure portal)

Now that we have a Function App, we can create a Function in it.

  1. Navigate to the Function App in the Azure portal
  2. Click on the Functions menu
  3. Select Create. This opens the Create function blade
    1. Leave the Development environment to "Develop in portal"
    2. Select the Azure Cosmos DB Trigger template
    3. Fill in a Name for the Function
    4. Click New for the Cosmos DB Connection
      1. Select Azure Cosmos DB Account
      2. Pick your existing Azure Cosmos DB and click OK
    5. Next, fill in the name of the Database that is in the Azure Cosmos DB account
    6. Also, fill in the name of the Collection that you want to use
    7. Leave the settings for the leases collection as they are
    8. Click Create to create the Function

(Create a Function in the Azure portal)

You'll be automatically redirected to the Function when it is created. From there, you can test it.

  1. In the Function, click on the Code + Test menu
  2. Add a line to the Function code to output the complete JSON string of the Azure Cosmos DB document, like in the code below:
#r "Microsoft.Azure.DocumentDB.Core"
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;

public static void Run(IReadOnlyList<Document> inputDocuments, ILogger log)
{
    if (inputDocuments != null && inputDocuments.Count > 0)
    {
        log.LogInformation("Documents modified " + inputDocuments.Count);
        log.LogInformation("First document Id " + inputDocuments[0].Id);
        log.LogInformation("Document string " + inputDocuments[0].ToString());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  1. Click Save to compile the code
  2. Now Select the Logs button at the bottom. This will connect the console to the streaming log. We'll use this to monitor the Function
  3. Next, go to the existing Azure Cosmos DB
  4. Navigate to the Data Explorer menu
  5. Select the collection that you coupled to the Azure Function earlier
  6. Add or update an item. This should trigger the Function

(Azure Cosmos DB Data Explorer)

  1. Go back to the Function and check the Logs console. This should now show that the Function executed. It was triggered by the items in Azure Cosmos DB, and it should show the ID of the item and the entire content in JSON format. Without writing any plumbing code, this Function gets triggered by changes in Azure Cosmos DB, and receives the changed items.

(Azure Function triggered by Azure Cosmos DB)

# Conclusion

Azure Functions (opens new window) triggers and bindings (opens new window) make it easy to execute Functions when external events happen, and to receive and send data from and to services. This is possible without writing any plumbing code to connect to services. Go and check it out!