This repository provides a code sample in .NET on how you might use a combination of Azure Functions and Cosmos DB to implement an inventory management process.
This demo contains 2 alternative ways to implement inventory management.
- AsyncInventory Function (Http Trigger - Rest API)
- InventoryProcessor Function (CosmosDB Trigger)
- inventoryLedger container holds inventory events
- inventorySnapshot container holds inventory snapshots (current inventory position)
- leases container used by CosmosDB Trigger
- SyncInventory Function (Http Trigger - Rest API)
- syncInventory container holds inventory events and shapshots as separate documents
Setup shell was tested on WSL2 (Ubuntu 22.04.2 LTS)
-
Install Zip: sudo apt install zip
The setup will provision and configure all the resources required.
-
Sign in with Azure CLI
az login
-
Clone the repo
git clone https://github.com/fonsecamar/cosmos-inventory-demo.git cd cosmos-inventory-demo/deploy/ -
Run setup.sh with the appropriete parameters. Keep the API's URIs prompted when completed.
Provide a non-existent resource group name. Setup will provision.
#SAMPLE
#./setup.sh 00000000-0000-0000-0000-000000000000 rg-my-demo SouthCentralUS
./setup.sh <subscription id> <resource group> <location>Setup has some pause stages. Hit enter to continue when prompted.
It takes around 3min to provision and configure resoures.
Resources created:
- Resource group
- Azure Blob Storage
- Azure Cosmos DB account (1 database with 1000 RUs autoscale shared with 4 containers)
- Azure Functions Basic Plan
- Azure Log Analytics Workspace
- Azure Application Insights
Microsoft Fabric is not created or configured by this automation.
For performance/load testing, use containers with dedicated throughput and Elastic Premium Functions.
You can call Function APIs from Azure Portal or your favorite tool.
Async and Sync calls are similar. Just change the api path: /CreateAsyncInventoryEvent or /CreateSyncInventoryEvent, /GetAsyncSnapshot or /GetSyncSnapshot.
-
Creates initial inventory of a product
InventoryUpdated event will create or patch InventorySnapshot increasing onHand and availbleToSell quantities.
curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \ --header 'Content-Type: application/json' \ --data-raw '{ "pk": "1-1000", "eventType": "InventoryUpdated", "eventDetails": { "productId": "1000", "nodeId": "1", "onHandQuantity": 1000 } }'
-
Notifies items reserved
ItemReserved event will patch InventorySnapshot increasing activeCustomerReservations and decreasing availbleToSell quantities. Reservations can only occur if reservedQuantity < availableToSell.
curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \ --header 'Content-Type: application/json' \ --data-raw '{ "pk": "1-1000", "eventType": "ItemReserved", "eventDetails": { "productId": "1000", "nodeId": "1", "reservedQuantity": 10 } }'
-
Notifies order shipped
OrderShipped event will patch InventorySnapshot decreasing activeCustomerReservations and onHand quantities. Shippments can only occur if shippedQuantity <= activeCustomerReservations.
curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \ --header 'Content-Type: application/json' \ --data-raw '{ "pk": "1-1000", "eventType": "OrderShipped", "eventDetails": { "productId": "1000", "nodeId": "1", "shippedQuantity": 10 } }'
-
Notifies order cancelled
OrderCancelled event will patch InventorySnapshot decreasing activeCustomerReservations and increasing availableToSell quantities. Cancellations can only occur if cancelledQuantity <= activeCustomerReservations.
curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \ --header 'Content-Type: application/json' \ --data-raw '{ "pk": "1-1000", "eventType": "OrderCancelled", "eventDetails": { "productId": "1000", "nodeId": "1", "cancelledQuantity": 10 } }'
-
Notifies order returned
OrderReturned event will patch InventorySnapshot increasing returned and onHand quantities.
curl --request POST "https://api-funcinv<suffix>.azurewebsites.net/api/CreateAsyncInventoryEvent" \ --header 'Content-Type: application/json' \ --data-raw '{ "pk": "1-1000", "eventType": "OrderReturned", "eventDetails": { "productId": "1000", "nodeId": "1", "returnedQuantity": 10 } }'
-
Get snapshot
curl --request GET "https://api-funcinv<suffix>.azurewebsites.net/api/GetAsyncSnapshot/1-1000"
- Delete the Resource Group to destroy all resources
If you find any errors or have suggestions for changes, please be part of this project!
- Create your branch:
git checkout -b my-new-feature - Add your changes:
git add . - Commit your changes:
git commit -m '<message>' - Push your branch to Github:
git push origin my-new-feature - Create a new Pull Request 😄

