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.


To complete this tutorial:

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


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 Azure Cosmos DB's API for MongoDB, 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. Go to the Azure portal to create an Azure Cosmos DB account. Search for and select Azure Cosmos DB.

    The Azure portal Databases pane

  2. Select Add.

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

    Setting Value Description
    Subscription Subscription name Select the Azure subscription that you want to use for this Azure Cosmos account.
    Resource Group Resource group name Select a resource group, or select Create new, then enter a unique name for the new resource group.
    Account Name A unique name Enter a name to identify your Azure Cosmos account. Because is appended to the name that you provide to create your URI, use a unique name.

    The name can only contain lowercase letters, numbers, and the hyphen (-) character. It must be between 3-31 characters in length.
    API The type of account to create Select Core (SQL) to create a document database and query by using SQL syntax.

    The API determines the type of account to create. Azure Cosmos DB provides five APIs: Core (SQL) and MongoDB for document data, Gremlin for graph data, Azure Table, and Cassandra. Currently, you must create a separate account for each API.

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

    The new account page for Azure Cosmos DB

  4. Select Review + create. You can skip the Network and Tags sections.

  5. Review the account settings, and then select Create. It takes a few minutes to create the account. Wait for the portal page to display Your deployment is complete.

    The Azure portal Notifications pane

  6. Select Go to resource to go to the Azure Cosmos DB account page.

    The Azure Cosmos DB account page

Create an Azure Function app

  1. From the Azure portal menu, select Create a resource.

    Add resource by using the Azure portal menu

  2. In the New page, select Compute > Function App.

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


    Setting Suggested value Description
    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.
    Function App name Globally unique name Name that identifies your new function app. Valid characters are a-z (case insensitive), 0-9, and -.
    Publish Code Option to publish code files or a Docker container.
    Runtime stack Preferred language Choose a runtime that supports your favorite function programming language. Choose .NET for C# and F# functions.
    Region Preferred region Choose a region near you or near other services your functions access.

    Select the Next : Hosting > button.

  4. Enter the following settings for hosting.


    Setting Suggested value Description
    Storage account 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 meet the storage account requirements.
    Operating system Preferred operating system An operating system is pre-selected for you based on your runtime stack selection, but you can change the setting if necessary.
    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.

    Select the Next : Monitoring > button.

  5. Enter the following settings for monitoring.


    Setting Suggested value Description
    Application Insights Default Creates an Application Insights resource of the same App name in the nearest supported region. By expanding this setting, you can change the New resource name or choose a different Location in an Azure geography where you want to store your data.

    Select Review + Create to review the app configuration selections.

  6. Select Create to provision and deploy the function app.

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

    Deployment notification

  8. Select Go to resource to view your new function app. You can also select Pin to dashboard. Pinning makes it easier to return to this function app resource from your dashboard.

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 Cosmos DB extension 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.
    Container name Items Name of container to be monitored.
    Create lease container if it doesn't exist Checked The container doesn't already exist, so create it.
    Database name Tasks Name of database with the container 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 container in the Tasks database.

Create the Items container

  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. Under SQL API, choose Tasks database and select New Container.

    Create a container

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

    Define the Tasks container

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

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

Test the function

  1. Expand the new Items container in Data Explorer, choose Items, then select New Item.

    Create an item in Items container

  2. Replace the contents of the new item 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. For more information about Azure Cosmos DB triggers, see Azure Cosmos DB bindings for Azure Functions.

Now that you have created your first function, let's add an output binding to the function that writes a message to a Storage queue.