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
Connect the pizza site to your new database
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.
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
- 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.
- Connect your database to the pizza site
- Setting up the function based on the template
- Modifying the template to fit your needs
Set up database in Azure
In this section, you you will set up the database.
- In a browser, go to the Azure portal
-
Type in
Cosmos DB
in the search bar and select the Azure Cosmos DB service -
Click
Create
and select the Create for the Core (SQL) API -
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
- Resource group:
-
Click review and Create
- Once validation has passed. Click create.
While your database is being provisioned enjoy a coffee break! :)
Connect the pizza site to your new database
-
In a browser, navigate to the Azure Portal and open your newly created Cosmos DB resource.
-
In the left menu under Settings click
Keys
. -
Copy the read-write primary connection string. This will be required as input in the pizza site.
-
In a browser, go to https://pizzaapp.z1.web.core.windows.net/
-
In your db connection string into the input box in the upper right corner and click
Update
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
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
- In VS Code, under the
Workspace
section of the Azure extension, selectAdd
andCreate Function
.
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
-
5. Select the Azure subcription with the cosmos database: If you are using the demo cosmos database instance use “Microsoft Azure Sponsorship”
-
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)
-
8. Set the collection name to “ratings” (“Storage” is the name of container created by the pizza ranker API)
If promted for storage account. Press “Skip for now”, it is not needed for this workshop.
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
-
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.
-
Print the content of the rating in the console.
Hint: The toString() method on the element will return a json representation of the entry.
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; }
}