https://github.com/fun-with-serverless/rustifying-serverless
How to use Rust in your existing Serverless projects
https://github.com/fun-with-serverless/rustifying-serverless
Last synced: about 2 months ago
JSON representation
How to use Rust in your existing Serverless projects
- Host: GitHub
- URL: https://github.com/fun-with-serverless/rustifying-serverless
- Owner: fun-with-serverless
- License: apache-2.0
- Created: 2023-08-30T08:37:02.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-13T08:22:45.000Z (over 1 year ago)
- Last Synced: 2025-04-07T10:46:05.279Z (3 months ago)
- Language: JavaScript
- Size: 1.04 MB
- Stars: 45
- Watchers: 0
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# “Rustifying” serverless: Boost AWS Lambda performance with Rust (COM306)
## Introduction
This repository serves as a companion to my talk at AWS ReInvent. The talk, titled "“Rustifying” serverless: Boost AWS Lambda performance with Rust", explores how to leverage the performance advantages of Rust in serverless AWS Lambda functions. For more details and to watch the talk, visit [ReInvent Talk](https://www.youtube.com/watch?v=Mdh_2PXe9i8).## Background Story
### The Journey
- As a company focused on developing AWS management tools, we started with a Minimum Viable Product (MVP) for S3 bucket management.
- Initially, the product was open only to our most loyal customers, and we received overwhelmingly positive feedback.
- Eventually, we opened the product to all of our users.
- At its peak, the service catered to thousands of clients and tens of thousands of users.
### Challenges
- We started experiencing performance bottlenecks and increased IT spending.
- Major architectural changes like adding cache and improving algorithms were implemented.### The Decision
- We reached a point where our existing runtime, Python, was not sufficient for our performance needs.
- Thus, we decided to leverage Rust for its performance advantages.This repository illustrates the journey that led us to this point.
## Architecture
- The architecture consists of an API Gateway with a Lambda authorizer.
- Multiple Lambda functions are connected to the API Gateway. These functions interact with services like S3 and DynamoDB.
- We have multiple runtimes, mostly Python, but also NodeJS.## Ways to Integrate Rust into Your Serverless Workload
There are primarily three ways to integrate Rust:### 1. Rust Bindings
#### Python
- Use Pyo3 with maturin to create a `.whl` package for use in your Python Lambda.
- Example code can be found under [s3-ops-rust-pyo3-lib](./s3-ops-rust-pyo3-lib).#### NodeJS
- Use NAPI-RS to package and build your code. A one stop CLI, unlike Python where you need to use teo tools.
- Examples can be found under [s3-ops-rust-napi-lib](./s3-ops-rust-napi-lib).**Benefits**: Speed of development and no need to rewrite the Lambda function.
### 2. Rewrite the Lambda
- Use `cargo-lambda` and AWS SAM to deploy a full-fledged Rust Lambda.
- Example code can be found under [s3-admin-app/authorizer_rust](./s3-admin-app/authorizer_rust).
- **Benefits**: Reduced cold starts, thanks to the Rust runtime.### 3. Use Extensions
- Use extensions to address cross-cutting concerns, such as analytics reporting, using a fast language like Rust.
- Example code can be found under [analytics-extension](./analytics-extension).
- **Benefits**: Reduce Lambda latency by offloading tasks to an external process.## Building the Example
- This example comes with a devcontainer for both Rust and Python development.
- In case you are not using DevContainers, make sure you have the following prerequisites:
- Python 3.11
- Rust - latest version
- Poetry
- Poe
- AWS SAM
- NodeJS 20
- AWS CLI
- NAPI-RS
- Prepare the NodeJS Rust Library by running:
```
cd s3-ops-rust-napi-lib
npm i
```
- We use `poetry` for build management. To build and deploy, run:
```bash
# Install dev tools used for rust compilation.
poetry install --only=rust-dev-tools
# Build the rust package
poe build-python-lib
poe build-node-lib
# Build and deploy the extension
poe build-and-deploy-extension
```
- Update [s3-asmin-app/template.yaml](./s3-admin-app/template.yaml) with the ARN of the extension under `Globals/Layers`.
- Build the application and deploy it - `poe build-and-deploy-app`
- To create a new user in the 'users' table, follow the AWS SAM output. For example `aws dynamodb put-item --table-name ...`
- Make API calls to the various APIs as defined in the AWS SAM output.## Local development
The main application resides in `s3-admin-app`, Rust bindings are in `s3-ops-rust-lib` and the extension in `analytics-extension`.
```bash
# Install dev tools used for rust compilation.
poetry install --only=rust-dev-tools
# Build the rust package
poe build-python-lib
poe build-node-lib
# Add the local rust package
poetry add .rust-lib/s3_ops_rust-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl --group dev
poetry install
```