AWS Rust Lamdba function with DynamoDB
Project Link: Click this link
Description
This is a simple example of a Rust Lambda function that uses DynamoDB. The function will be triggered by an API Gateway event and will store the request body in a DynamoDB table.
This project idea is motivated by the requirements of another project, Wastewater Treatment Plant, where there's a necessity to store inference data generated by a machine learning model in the cloud. The objective is to create a system that automatically triggers a function responsible for storing the data and comparing the predictions with the ground truth. Subsequently, the system adds a boolean value (true or false) to the "correct" column in the DynamoDB table. This process is carried out in addition to handling JSON payloads provided by the user or API Gateway.
The following is an example of the request body that the function expects:
{
"wwtp": "Wwtp_name",
"ground_truth": "Yes or No",
"prediction": "Wes or No",
}
The following is an example of the table in Excel format:
Flowchart of the function
Prerequisites and Dependencies
-
Code is developed with Rust
-
Function deployed to AWS Lambda
- Upload the function to AWS Lambda
- using .zip file
- OR using
cargo lambda deploy
- Upload the function to AWS Lambda
-
Using AWS CLI to manage AWS services
- Configure the AWS CLI with
aws configure
- OR set the environment variables
- Configure the AWS CLI with
-
Managing the function's permissions with AWS IAM
-
Create a user or assign the role with the following permissions:
AWSLambdaFullAccess
AmazonDynamoDBFullAccess
AWSLambdaDynamoDBExecutionRole
-
Set the user's access key and secret key
- using
aws configure
- or setting the environment variables
AWS_ACCESS_KEY_ID
,AWS_SECRET_ACCESS_KEY
, andAWS_DEFAULT_REGION
with
export AWS_ACCESS_KEY_ID=your_access_key export AWS_SECRET_ACCESS_KEY=your_secret_key export AWS_DEFAULT_REGION=your_region
- using
-
-
Storing information to AWS DynamoDB table
- Create a table with the name matching the
TABLE_NAME
in themain.rs
file - The table should have a primary key
- Create a table with the name matching the
- Using AWS API Gateway to trigger the function and handle requests for inputing data to the DynamoDB table
Build and Deploy
- Build the function
cargo build --release
- Create a .zip file
cargo lambda build --release
- Deploy the function to AWS Lambda
cargo lambda deploy
Test the function
- Trigger the function using the AWS Lambda console
- OR use the AWS API Gateway to trigger the function
Screenshots
-
AWS Lambda functionality
-
AWS DynamoDB table
Even though we did not specify the "correct" column in the request body, the function automatically adds the "correct" column with a boolean value (true or false) to the DynamoDB table. This is a feature of the function that is shown in the screenshot.
License
This project is licensed under the MIT License - see the LICENSE file for details.