AzureTipsAndTricks/blog/tip352.md

6.5 KiB

type title excerpt tags share date
post Tip 352 - How to trigger an Azure Function from Azure Cosmos DB Learn how to trigger an Azure Function from Azure Cosmos DB
Serverless
Databases
true 2022-2-9 08:00:00

::: tip

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

💡 Learn more : Azure Functions Triggers and Bindings.

📺 Watch the video : How to trigger an Azure Function from Azure Cosmos DB.

:::

How to trigger an Azure Function from Azure Cosmos DB

Azure Functions triggers and bindings

Azure Functions are great for running small workloads, and they can run serverless, which means that they scale automatically at a low price. They also offer triggers and bindings 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. They also help you send output to services, like inserting a row in an Azure SQL Database, 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 to start an Azure Function.

Prerequisites

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

  • An Azure subscription (If you don't have an Azure subscription, create a free account before you begin)
  • An exiting Azure Cosmos DB account with a database and container. Follow this quickstart to learn how to create one

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
  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. 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 triggers and bindings 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!