DEV Community

Cover image for Inventory Management with AWS Lambda λ
Justine Mae Macario
Justine Mae Macario

Posted on

Inventory Management with AWS Lambda λ

This blog describes what I did for a Simple Pharmacy Inventory Management System using AWS Lambda and other services!

📚Background: Checking and Restocking Process

A relative who wanted to open a pharmacy soon asked me about the idea of digitizing their processes. They mentioned the dread of checking inventories and restocking during their internship, so I started with that problem by incorporating AWS Lambda.

AWS Lambda lets you run code without managing servers. All that's expected from the client is the code in a language that Lambda supports. In this case, AWS Lambda is combined with AWS DynamoDB, Amazon SNS, and Amazon TimeStream.

📚Background: On AWS' Lambda Features

This AWS Service is ideal for rapid scaling. Lambda can be used for file processing (+ Amazon S3), stream processing (+Amazon Kinesis), and IoT/Mobile Backends (+API Requests, +AWS Amplify). A Lambda function also runs when needed and you only pay for the compute time consumed.


🔧Methods: Communication and Diagram

Hypothetical Scenario #100

I tried making an AWS Architecture Diagram for me and the client to grasp the concept.

🔧Methods: Services To Be Used

Compute

  • Lambda - update processing

Storage

  • DynamoDB - inventory data

  • AWS TimeStream - inventory analysis

  • AWS SNS - notification system for stock alerts

API Layer

  • API Gateway - receives requests

Security

  • Amazon Cognito - API request authentication

  • Amazon IAM - permissions and access controls for Lambda Functions

Monitoring

  • Amazon CloudWatch - system activity monitoring and logging.

MISC

  • Pharmacy's Application - inventory update and info

🔧Methods: Deployment Process

1.) AWS DynamoDB SetUp

Search DynamoDB in the AWS Management Console and create a table.
The table name depends on what the client needs so I'll use the following:

Table Name: "pha_Inventory"
Primary/Partition Key: "idProduct" (String)
Range/Sort Key: "quantity" (Number)

Sample Table

2.) AWS SNS Setup

Look up SNS in the Management Console and create a topic.

Topic Type(1): Standard
Name: "LowStockAlerts"

Trig1

Write down the ARNs or the Amazon Resource Names of the topic.

3.) Amazon TimeStream Setup

Switch back to the AWS Management Console and look for TimeStream. Create the database needed for the analysis:

Configuration: Standard
Name: "pha_Analytics"

Create a table within the database:

Name: "pha_InventoryLevels"
Components: Time, Dimensions, Measures

TimeStream SetUp

4.) AWS Cognito Setup

This includes setting up Cognito requires creating a user pool(stores user profile information), an app client (says what certain services/apps will interact with the user pool) , and initial users(the ones who'll use the system).

4.a.) User Pool

Create a user pool in AWS Cognito:

Sign In Attribute: Email
User Pool Name: Inventory System Users

4.b.) App Client

Within the User Pool, add an app client.

App Client Name: "InventoryUpdateClient"

4.c.) Initial Users

In the User Pool again, create a user.

This includes placing the chosen user's email for access (pharmacist/pharmacy staff).

5.) IAM Role Config.

From the Management Console, click the IAM Service and create a role. You'd have to choose AWS Lambda as a trusted entity and check the following policies:

AWSLambdaBasicExecutionRole [minimal permissions, write + execute logs]
AmazonDynamDBFullAccess [full access]
AmazonSNSFullAccess [full access]
AmazonTimestreamFullAccess [full access]

IAM Role Config

IAM Role Config

IAM Role Config

IAM Role Config

IAM Role Config

6.) Lambda Function

Place the JavaScript Code found in this Github Repository and update the ARNS (Amazon Resource Names) with the ones noted down.

Author from Scratch
Name: "InventoryUpdateFunction"
Runtime: Node.js (22.x)
Specific Role: "LambdaInventoryUpdateRole" (IAM Role)

IAM Role Config

7. DynamoDB Trigger Setup

This part includes adding an event trigger found in the Repository. Open AWS the Lambda Console and go to functions overview. You'll see the "Add Trigger" dropdown and then choose the DynamoDB Option. Select the DynamoDB Table "pha_Inventory" and adjust the Batch Size accordingly. The starting position should be "Latest" and then enable the trigger.

Here's a snap of what the code looks like:

DynamoDB Trigger Setup

*Note: Access Restrictions ;)


📄Results: Testing

The (hypothetical) setup is finished for a Simple Pharmacy Inventory System!

Modify products in the DynamoDB Console and check if the stock alerts are sent to the AWS SNS Topic. Also, look into the CloudWatch Logs for the Lambda Triggers and the TimeStream Database for inventory updates.


💭Final Thoughts

🤯 Hoping I'll improve more the next time I do this.


Important Links:
GitHub Repo

Top comments (0)