This sample Inventory Management Microservice demonstrates how to manage inventory levels by reserving inventory when a new order is received from an Orders Microservice, alerts on low stock, and triggers purchase orders to replenish inventory.
Learn more about this workflow at Step Functions workflows collection: https://serverlessland.com/workflows/inventory-management-sam
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
-
Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/step-functions-workflows-collection
-
Change directory to the pattern directory:
cd inventory-management-sam
-
From the command line, use AWS SAM to build and deploy the AWS resources for the workflow as specified in the template.yaml file:
sam build sam deploy --guided
-
During the prompts:
- Enter a stack name
- Enter the desired AWS Region
- Enter your email address for notifications and purchase order approvals
- Allow SAM CLI to create IAM roles with the required permissions.
Once you have run
sam deploy --guided
mode once and saved arguments to a configuration file (samconfig.toml), you can usesam deploy
in future to use these defaults.
The sample Inventory Management Microservice consists of:
-
An Event Bridge Schedule which invokes a Lambda Function to send a
new-order-received
event every minute, to generate some sample data which would be sent by a hypothetical Order Microservice. This schedule is disabled initially, instructions on enabling it are in the Testing section below. -
An Event Bridge Rule handles the
new-order-received
event and persists it to an SQS Queue for durability. -
A
reserve-stock
Lambda function polls the SQS Queue and starts thereserve-stock
workflow. -
The
reserve-stock
workflow is an Express workflow as it should execute in under 5 minutes. It checks the Inventory DynamoDB table for the specified product and if the product status isIN STOCK
and there is sufficient quantity, the workflow starts these steps in parallel - demonstrating the Choice and Parallel State:- The inventory level is updated.
- A
stock-reserved
event is sent, which the Order Microservice would handle and continue with order processing. - The stock reservation is written to the
Inventory Reservation
table.
If there isn't sufficient stock the workflow starts these steps in parallel:
- A
create-purchase-order
event is sent. - A
stock-unavailable
event is sent, which the Order Microservice would handle and hold order processing until the stock was available - A stock unavailable notification is sent
-
The
create-purchase-order
workflow is a Standard workflow as it uses a Callback which is not supported by Express workflows and it could take longer than 5 minutes to complete as it waits for a response. This workflow is the target for thecreate-purchase-order
event, and it sends a purchase order email and waits for a callback. If the purchase order isapproved
the Inventory Table is updated for the specified product - the stock level is incremented by the purchase order amount and the product status is updated toIN STOCK
. -
The
check-inventory-level
workflow is initiated when there are changes in the Inventory Table. DynamoDB Streams is enabled on the Inventory table to continuously monitor inventory levels. A Lambda function processes the stream records and starts thecheck-inventory-level
workflow which checks if the inventory level falls below the threshold for a product, and sends acreate-purchase-order
event. The purchase order event is handled by an Event Bridge Rule which starts the create purchase order workflow. In parallel, this workflow checks if the stock level is zero for the product and if so, updates the product status toOUT OF STOCK
and sends an alert that the stock level is zero.
Invoke the SendNewOrderReceivedLambda
by getting the lambda name from the outputs after deployment or using (just substitute the stack name you used (default is sam-app
)). This will generate an event containing a random product and quantity.
aws lambda invoke --function-name $(aws cloudformation describe-stacks --stack-name <stack-name> --query "Stacks[*].Outputs[?OutputKey=='SendNewOrderReceivedLambdaName'].OutputValue" --output text) response.json
The event will trigger the reserve-stock
workflow which you can view from the Step Functions console. If there is enough stock the reservation will be written to the Inventory Reservation Table. You can view the items in the DynamoDB table in the console or by using the below command:
aws dynamodb scan --table-name $(aws cloudformation describe-stacks --stack-name <stack-name> --query "Stacks[*].Outputs[?OutputKey=='InventoryReservationTableName'].OutputValue" --output text)
If there isn't enough stock the create-purchase-order
will be triggered and you will receive a Purchase Order request email. The check-inventory-level
workflow will be triggered for any updates to the Inventory Table.
Enable the Event Bridge Schedule from the Event Bridge Scheduler console
You can view the reserve-stock
, create-purchase-order
and check-inventory-level
workflows from the Step Functions console as the reserve-stock
Workflow will be triggered every minute. Disable the Event Bridge Scheduler schedule to stop new-order-received
events from being sent and incurring additional costs in the console or delete the stack.
- Delete the stack
sam delete
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0