Skip to main content Link Search Menu Expand Document (external link)

Creating an Azure Function with a Cosmos DB trigger

In this task we will be creating an Azure Functions with a Cosmos DB trigger. Cosmos DB is a fully managed NoSQL database for modern app development. As with all databases you can read, insert, update and delete elements in it. Including a Cosmos DB trigger to a function will result in the function being executed whenever a document is inserted or updated in the database.

Table of contents

The case

Set up database in Azure

Connect the pizza site to your new database

Create a new Azure Function

Complete setup of function

Modify Cosmos DB trigger function

The case

In this task you will be working with a website for rating pizza. The site collects ratings for various pizzas and users can rate each pizza using emojis. Each emojis translates into a score between 0 to 4 and the rating is stored in Cosmos DB. Your task is to create an Azure Function that is executed whenever a new rating is given on the web site.

website overview

rating

For the purpose of this workshop, each instance of the website will store the ratings in a database determined by the user. This way, you can test your function by adding a rating to the site and these only being registered in your database, hence only triggering your function.

For the website to identify your database, you will have to provide the connection string to your database.

The case will be completed in four steps

  1. Setting up a database in Azure: This database will be set up in your personal account and is where the pizza ratings will be persisted.
  2. Connect your database to the pizza site
  3. Setting up the function based on the template
  4. Modifying the template to fit your needs

Set up database in Azure

In this section, you you will set up the database.

  1. In a browser, go to the Azure portal
  2. Type in Cosmos DB in the search bar and select the Azure Cosmos DB service

    Azure Portal Search bar

  3. Click Create and select the Create for the Core (SQL) API

    CosmosDB select API

  4. Set the basic configurations for the database

    • Resource group: learning-functions-rg
    • Account Name: learning-functions-[insert initials]-db
    • Location: (Europe) West Europe
    • Capacity mode: Serverless
  5. Click review and Create

    Portal confirm database

  6. Once validation has passed. Click create.

While your database is being provisioned enjoy a coffee break! :)

Connect the pizza site to your new database

  1. In a browser, navigate to the Azure Portal and open your newly created Cosmos DB resource.

  2. In the left menu under Settings click Keys. Keys settings in portal

  3. Copy the read-write primary connection string. This will be required as input in the pizza site.

  4. In a browser, go to https://pizzaapp.z1.web.core.windows.net/

  5. In your db connection string into the input box in the upper right corner and click Update Pizza rating site

You should now be able to add ratings to the pizzas on the website. Which one is your favorite?

You should see a new container ratings in your database containing the pizza ratings Rating container

In the next step we will be creating the function that will respond to the inputting of ratings on this site.

Create a new Azure Function

  1. In VS Code, under the Workspace section of the Azure extension, select Add and Create Function.

Create project

You will now be prompted for configurations for the project and login to Azure. Input the following values:

  • 1. Template for function: Azure Cosmos DB trigger

  • 2. Function name: RatingTrigger

  • 3. Namespace: LearningFunctions.RatingTrigger

  • 4. App settings: Create new local app settings Create local app settings

  • 5. Select the Azure subcription with the cosmos database: If you are using the demo cosmos database instance use “Microsoft Azure Sponsorship” Select subscription

  • 6. Select the Comsos account to use: Your newly generated db account should show up in the list, if this is not the case sign out from Azure in VS Code (Ctrl+Shift+P) and log back in before trying again.

  • 7. Set the database name to “storage” (“Storage” is the name of database created by the pizza ranker API) Create local settings

  • 8. Set the collection name to “ratings” (“Storage” is the name of container created by the pizza ranker API) Create local settings

If promted for storage account. Press “Skip for now”, it is not needed for this workshop. Prompt

Your function has now been setup. Your local.settings.json should be updated with your cosmos connection string and a file RatingTrigger.cs added to your project folder.

RatingTrigger.cs is the function trigger. Within the CosmosDBTrigger attribute you should be able to see the values configured in the previous steps.

Running the function

Your function should now be ready to go and you can run it by typing the cmd func start in the terminal.

Each time a rating is given on the web site, you should see activity in your console with the number of documents modified.

Question

The template function only accesses the first element in the input collection. In what cases would the collection hold more than one element?

Modify Cosmos DB Trigger function

  1. Ensure that all changes to the ratings results in a log line in the console.

    Hint: Try looping through the input collection with a ForEach loop.

    Code hint

  2. Print the content of the rating in the console.

    Hint: The toString() method on the element will return a json representation of the entry.

    Code hint

3) Print a a special message if a pizza gets the best rating(😍)

Hint: Use JsonSerializer from the System.Text.Json namespace to deserialize the input from the trigger to a rating object

   public class Rating
   {
     public Guid id { get; set; }

     public int pizzaId { get; set; }

     public int score { get; set; }

     public DateTime created { get; set; }
   }

Code hint