Azure Functions Basics - microsoft-campus-community/workshop-shopping-list-bot GitHub Wiki
create one first.
Prerequisite: You are going to use the CosmosDB in the Azure Functions. If you do not have a CosmosDB in Azure yet,General Information
Azure Function is the serverless cloud-based computing offering by Microsoft. It allows you to run code in the cloud only when it is used. The underlying infrastructure still consists of servers. However, you as the developer do not need to worry about them. A metaphor that might help you or your audience understand it is a professional pizza oven which stands for running your code on regular servers, and the oven you have at your home, which stands for Azure Function. The professional pizza oven has its purpose. It can run all day and is always ready to go when a customer needs a pizza. Nevertheless, for most people, a regular oven at home is enough. They might take some time first to heat up before you can use them. However, they provide more functionality and you do not waste energy and money because your oven is only on when you are baking. The same applies to Functions. When they are not running, it will take a second to start-up. When they are running, they are simpler to use and only run as long as you use them, after which they will pause.
Shopping List API
In the shopping list Functions, a Cosmos DB is used to persist the shopping list items. The bot could use the Cosmos DB API directly. However, this might not be the best software design. Imagine you want to offer the chat participants to also manage the shopping list from a native app or a web app. Then you will need to rewrite a lot of code to access the Cosmos DB that is also in the bot. To decouple both things, you will develop a RESTful API with Azure Functions to handle the shopping list. Then the bot can use the API to persist items in a shopping list.
Getting started with Azure Functions
For your next project, when you need an Azure Functions Project from scratch, you should check out Develop Azure Functions using Visual Studio Code. This is how the Azure Function project for the shopping list API was created. The Visual Studio Code Azure Functions extension is helpful when adding new Functions to the project. If you forked this repository for the workshop, then the Function project's file structure is already there.
Code Structure
- The root directory of the
functions
folder contains a lot of configuration for NodeJS. If you have built NodeJS projects before, this will be familiar to you. If you do not, these configuration files are not so important to get started. Only thehost.json
andlocal.settings.json
will become more important later. You will learn more about them when they are needed. - The
models
folder contains classes for the shopping list domain. TheItemDb
andItem
classes represent a single food in the shopping list. TheItemDb
has noid
and is used to insert an item in the database. It has noid
so that the database will automatically generate a new id when inserting.Item
extendsItemDb
because it is the same, except it has anid
, so we can use it for any other database operations. TheUnit
class is used to store a unit (e.g., 2 pounds) for an item. - The
services
folder contains theCosmosDBService
class that implements all the logic to work on the shopping list database. You will look at it in more detail in the next part. - Then there is a folder for each Function (i.e., each endpoint of the shopping list API). They are in the
GetItemsFunction
,RemoveAllItemsFunction
,RemoveItemById
andUpdateItemFunction
. You will look at them in a bit.