Create a function triggered by Azure Cosmos DB

Learn how to create a function triggered when data is added to or changed in Azure Cosmos DB. To learn more about Azure Cosmos DB, see Azure Cosmos DB: Serverless database computing using Azure Functions.

View message in the logs.

Prerequisites

To complete this tutorial:

  • If you don't have an Azure subscription, create a free account before you begin.

Note

Azure Cosmos DB bindings are only supported for use with the SQL API. For all other Azure Cosmos DB APIs, you should access the database from your function by using the static client for your API, including MongoDB API, Cassandra API, Gremlin API, and Table API.

Create an Azure Cosmos DB account

You must have an Azure Cosmos DB account that uses the SQL API before you create the trigger.

  1. In a new browser window, sign in to the Azure portal.
  2. Select Create a resource > Databases > Azure Cosmos DB.

    The Azure portal Databases pane

  3. On the Create Azure Cosmos DB Account page, enter the basic settings for the new Azure Cosmos DB account.

    Setting Value Description
    Subscription Your subscription Select the Azure subscription that you want to use for this Azure Cosmos DB account.
    Resource Group Create new

    Then enter the same unique name as provided in ID
    Select Create new. Then enter a new resource-group name for your account. For simplicity, use the same name as your ID.
    Account Name Enter a unique name Enter a unique name to identify your Azure Cosmos DB account. Because documents.azure.com is appended to the ID that you provide to create your URI, use a unique ID.

    The ID can only contain lowercase letters, numbers, and the hyphen (-) character. It must be between 3 and 31 characters in length.
    API Core(SQL) The API determines the type of account to create. Azure Cosmos DB provides five APIs: Core(SQL) for document databases, Gremlin for graph databases, MongoDB for document databases, Azure Table, and Cassandra. Currently, you must create a separate account for each API.

    Select Core(SQL) because in this article you create a document database and query by using SQL syntax.

    Learn more about the SQL API.
    Location Select the region closest to your users Select a geographic location to host your Azure Cosmos DB account. Use the location that's closest to your users to give them the fastest access to the data.

    Select Review+Create. You can skip the Network and Tags section.

    The new account page for Azure Cosmos DB

  4. The account creation takes a few minutes. Wait for the portal to display the Congratulations! Your Azure Cosmos DB account was created page.

    The Azure portal Notifications pane

Create an Azure Function app

  1. Select the New button found on the upper left-hand corner of the Azure portal, then select Compute > Function App.

    Create a function app in the Azure portal

  2. Use the function app settings as specified in the table below the image.

    Define new function app settings

    Setting Suggested value Description
    App name Globally unique name Name that identifies your new function app. Valid characters are a-z, 0-9, and -.
    Subscription Your subscription The subscription under which this new function app is created.
    Resource Group myResourceGroup Name for the new resource group in which to create your function app.
    OS Windows Serverless hosting is currently only available when running on Windows. For Linux hosting, see Create your first function running on Linux using the Azure CLI.
    Hosting plan Consumption plan Hosting plan that defines how resources are allocated to your function app. In the default Consumption Plan, resources are added dynamically as required by your functions. In this serverless hosting, you only pay for the time your functions run. When you run in an App Service plan, you must manage the scaling of your function app.
    Location West Europe Choose a region near you or near other services your functions access.
    Runtime stack Preferred language Choose a runtime that supports your favorite function programming language. Choose .NET for C# and F# functions.
    Storage Globally unique name Create a storage account used by your function app. Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only. You can also use an existing account, which must meets the storage account requirements.
    Application Insights Default Application Insights is enabled by default. Choose a location near your function app.
  3. Select Create to provision and deploy the function app.

  4. Select the Notification icon in the upper-right corner of the portal and watch for the Deployment succeeded message.

    Define new function app settings

  5. Select Go to resource to view your new function app.

Tip

Having trouble finding your function apps in the portal, try adding Function Apps to your favorites in the Azure portal.

Next, you create a function in the new function app.

Create Azure Cosmos DB trigger

  1. Expand your function app and click the + button next to Functions. If this is the first function in your function app, select In-portal then Continue. Otherwise, go to step three.

    Functions quickstart page in the Azure portal

  2. Choose More templates then Finish and view templates.

    Functions quickstart choose more templates

  3. In the search field, type cosmos and then choose the Azure Cosmos DB trigger template.

  4. If prompted, select Install to install the Azure Storage extension any dependencies in the function app. After installation succeeds, select Continue.

    Install binding extensions

  5. Configure the new trigger with the settings as specified in the table below the image.

    Create the Azure Cosmos DB triggered function

    Setting Suggested value Description
    Name Default Use the default function name suggested by the template.
    Azure Cosmos DB account connection New setting Select New, then choose your Subscription, the Database account you created earlier, and Select. This creates an application setting for your account connection. This setting is used by the binding to connection to the database.
    Collection name Items Name of collection to be monitored.
    Create lease collection if it doesn't exist Checked The collection doesn't already exist, so create it.
    Database name Tasks Name of database with the collection to be monitored.
  6. Click Create to create your Azure Cosmos DB triggered function. After the function is created, the template-based function code is displayed.

    Cosmos DB function template in C#

    This function template writes the number of documents and the first document ID to the logs.

Next, you connect to your Azure Cosmos DB account and create the Items collection in the Tasks database.

Create the Items collection

  1. Open a second instance of the Azure portal in a new tab in the browser.

  2. On the left side of the portal, expand the icon bar, type cosmos in the search field, and select Azure Cosmos DB.

    Search for the Azure Cosmos DB service

  3. Choose your Azure Cosmos DB account, then select the Data Explorer.

  4. In Collections, choose taskDatabase and select New Collection.

    Create a collection

  5. In Add Collection, use the settings shown in the table below the image.

    Define the taskCollection

    Setting Suggested value Description
    Database ID Tasks The name for your new database. This must match the name defined in your function binding.
    Collection ID Items The name for the new collection. This must match the name defined in your function binding.
    Storage capacity Fixed (10 GB) Use the default value. This value is the storage capacity of the database.
    Throughput 400 RU Use the default value. If you want to reduce latency, you can scale up the throughput later.
    Partition key /category A partition key that distributes data evenly to each partition. Selecting the correct partition key is important in creating a performant collection.
  6. Click OK to create the Items collection. It may take a short time for the collection to get created.

After the collection specified in the function binding exists, you can test the function by adding documents to this new collection.

Test the function

  1. Expand the new taskCollection collection in Data Explorer, choose Documents, then select New Document.

    Create a document in taskCollection

  2. Replace the contents of the new document with the following content, then choose Save.

     {
         "id": "task1",
         "category": "general",
         "description": "some task"
     }
    
  3. Switch to the first browser tab that contains your function in the portal. Expand the function logs and verify that the new document has triggered the function. See that the task1 document ID value is written to the logs.

    View message in the logs.

  4. (Optional) Go back to your document, make a change, and click Update. Then, go back to the function logs and verify that the update has also triggered the function.

Clean up resources

Other quick starts in this collection build upon this quick start. If you plan to work with subsequent quick starts, tutorials, or with any of the services you have created in this quick start, do not clean up the resources.

Resources in Azure refers to function apps, functions, storage accounts, and so forth. They are grouped into resource groups, and you can delete everything in a group by deleting the group.

You created resources to complete these quickstarts. You may be billed for these resources, depending on your account status and service pricing. If you don't need the resources anymore, here's how to delete them:

  1. In the Azure portal, go to the Resource group page.

    To get to that page from the function app page, select the Overview tab and then select the link under Resource group.

    Select the resource group to delete from the function app page.

    To get to that page from the dashboard, select Resource groups, and then select the resource group that you used for this quickstart.

  2. In the Resource group page, review the list of included resources, and verify that they are the ones you want to delete.

  3. Select Delete resource group, and follow the instructions.

    Deletion may take a couple of minutes. When it's done, a notification appears for a few seconds. You can also select the bell icon at the top of the page to view the notification.

Next steps

You have created a function that runs when a document is added or modified in your Azure Cosmos DB.

Learn how to create functions with other kinds of triggers or how to integrate functions with other Azure services.

For more information about Azure Cosmos DB triggers, see Azure Cosmos DB bindings for Azure Functions.