title | description | services | documentationcenter | author | manager | ms.assetid | ms.service | ms.devlang | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Create a function triggered by Azure Cosmos DB | Microsoft Docs |
Use Azure Functions to create a serverless function that is invoked when data is added to a database in Azure Cosmos DB. |
azure-functions |
na |
ggailey777 |
jeconnoc |
bc497d71-75e7-47b1-babd-a060a664adca |
azure-functions; cosmos-db |
multiple |
quickstart |
10/02/2018 |
glenga |
cc996988-fb4f-47 |
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.
To complete this tutorial:
- If you don't have an Azure subscription, create a free account before you begin.
Note
[!INCLUDE SQL API support only]
You must have an Azure Cosmos DB account that uses the SQL API before you create the trigger.
[!INCLUDE cosmos-db-create-dbaccount]
[!INCLUDE Create function app Azure portal]
Next, you create a function in the new function app.
-
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.
-
Choose More templates then Finish and view templates.
-
In the search field, type
cosmos
and then choose the Azure Cosmos DB trigger template. -
If prompted, select Install to install the Azure Storage extension any dependencies in the function app. After installation succeeds, select Continue.
-
Configure the new trigger with the settings as specified in the table below the image.
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. -
Click Create to create your Azure Cosmos DB triggered function. After the function is created, the template-based function code is displayed.
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.
-
Open a second instance of the Azure portal in a new tab in the browser.
-
On the left side of the portal, expand the icon bar, type
cosmos
in the search field, and select Azure Cosmos DB. -
Choose your Azure Cosmos DB account, then select the Data Explorer.
-
In Collections, choose taskDatabase and select New Collection.
-
In Add Collection, use the settings shown in the table below the image.
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. -
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.
-
Expand the new taskCollection collection in Data Explorer, choose Documents, then select New Document.
-
Replace the contents of the new document with the following content, then choose Save.
{ "id": "task1", "category": "general", "description": "some task" }
-
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. -
(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.
[!INCLUDE Next steps note]
You have created a function that runs when a document is added or modified in your Azure Cosmos DB.
[!INCLUDE Next steps note]
For more information about Azure Cosmos DB triggers, see Azure Cosmos DB bindings for Azure Functions.