Azure Cosmos DB's Change Feed feature triggers an event for Inserts and Updates in a collection. The easiest way to handle these events is, by executing an Azure Function. In this post, I will focus on creating an Azure Function for Azure Cosmos DB by using VsCode.
First, you need to be sure that you have the necessary plugins for VsCode. You need to have the Azure Tools extension and Azure Functions extensions to create Azure Functions in VsCode.
After installing these extensions, we are ready to set up a new Azure Function by using VsCode. You should have the Azure icon on the left side of the VsCode. You should see your resources and a couple of other tabs under that when you click on the Azure icon. Find the tab named Workspace and click on the flash button to add a new Azure Function to your Azure subscription.
When you click on the Flash button, VsCode will ask you which directory you would like to use for the new Azure Function. Just select a new folder and click Select to continue. After that, you need to answer a couple of questions about the function and your Cosmos DB account. The first question is which language you like to use in the Azure Function.
The next question is about .NET version you like to use in this new Azure function.
The next question is important. This will determine which Azure function should trigger this Azure function. Select the Azure Cosmos DB trigger template for this question.
Everything has names, our new function should have a name too. In the next question, give a name to the function.
Type a namespace name for your function in the next question.
We need to select an application setting file in the next question. You will save all the settings including Cosmos Db's connection string in this file. Since this is a new Azure Function, we need to create a new file. So I select Create new local app setting in the following question.
So far, we answered all the questions about the function environment. The next set of questions will be about your Azure environment. Select the Azure subscription you like to use for the new Azure Function in the next question.
You need to select which Azure Cosmos DB database account you like to use for this Azure Function in the next question.
Next, type the name of the database you like to connect.
Next, type the name of the collection you like to monitor. All inserts and updates will trigger this new Azure function.
If you like to debug your Azure Function locally, you need to select a storage account. Debug locally is one of the main reasons I used VsCode to create this Azure function. So, I click on the Select storage account button and select a storage account from the list. If you like, you can create a new one too.
After selecting a storage account, we need to tell VsCode where to display this new function. I select Open in the current window because I don't have anything open yet.
After that, you should see all the new files created for your new Azure Function on the left side. My new function named CallSkyNet is open on the right side too as you see in the following screenshot.
If you like to test this function, you can put a debug point in any lines under the Run function and select Run and Start Debugging from the menu or simply press F5 to start a debugging session. The Cosmos DB Container you picked when you were setting up this function, will be responsibly triggering this function. You need to insert or update an item in that container to start this function.
The easiest way to do this is using the Cosmos DB Data Explorer. In the following example, I update the Score property from 3 to 30. After that I click the Update button. The Change Feed feature will trigger the function.
A couple of seconds later, my function got called and my debug session started as you can see in the following screenshot.
No comments:
Post a Comment