How to trigger Azure Function by Azure Cosmos DB

Azure Functions is Microsoft’s serverless offering it lets you execute your code in a serverless environment without having to first create a VM or publish a web application.

Azure Cosmos DB is Microsoft’s globally distributed, multi-model database. With the click of a button, Azure Cosmos DB enables you to elastically and independently scale throughput and storage across any number of Azure’s geographic regions.

Step 1: Login into Azure Portal (http://portal.azure.com) and create a new Azure ComsosDBb data as shown below

0

Step 2: Now once the Azure Cosmos DB database gets created open it and create a collection in it as shown below.

01

Here you can see as of now it is present at Central India Data Center you can switch on as many locations as you want and Cosmos DB replicates your data transparently all by itself.

Now add a new collection name Conferences as shown below.

02.PNG

Now we can add an entry for a conference in our collection as shown below.

03.PNG

 

Now we will create Azure Function that will be triggered when we will add new entry in CosmosDB collection.


Step 3: Now go to Azure Portal dashboard Click Create a Resource from left menu search for Function App and click it, you will see the window as shown below fill the function name and other details and click crate as shown below.

1

Step 4: From your dashboard once this function gets created click on the function tile and open the function, click on the URL , in this case, it is https://conferencefunction.azurewebsites.net/ as shown below, your function app is running now.

2

Step 5: Now open your Cosmos DB database and click on Add Function as shown below.

addFx.PNG

Step 6: Now fill your function details as shown below and clicl on Save, and in couple of minutes your function will be created.

FillFxDetails.PNG

Step 7: Now this is how your function appears on the screen.

Fx.PNG

Step 8: Now open portal.azure.com in the new tab and open your cosmos db , now we will add a new conference in our collection and we can notice it will trigger our Azure Function.

DevTheory.PNG

Click on the save and now switch tab where you have your Azure function you will notice that Azure Function gets triggered as shown below.

FunctionTriggered.PNG

To Learn more about Azure Functions and Cosmos DB refer following links.

https://azure.microsoft.com/en-in/blog/introducing-azure-functions/

https://docs.microsoft.com/en-us/azure/cosmos-db/introduction

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.