This project demonstrates a fully serverless Rust backend architecture running on AWS Lambda, powered by:
- 🌐 Warp — ergonomic, high-performance HTTP server
- 🛢️ Diesel — production-ready ORM for PostgreSQL
- 🔌
warp_lambda— seamlessly adapt Warp filters for Lambda - 🧱 AWS SAM — serverless framework to define and deploy the stack
- 📦
libpqLambda layer — compiled from source to support Diesel's PostgreSQL backend on Lambda
Ensure the following tools are installed:
- Rust
- Cargo Lambda
- AWS CLI
- AWS SAM CLI
- Docker
- Docker Compose
act(optional, for running GitHub Actions locally)
.
├── rust_app/ # Rust source (Cargo.toml, main.rs, handlers, etc.)
├── libpq_layer/ # Compiled libpq.so + headers for Lambda
├── libpq_layer.zip # Zipped Lambda layer (optional manual upload)
├── build_libpq_layer.sh # Script to compile the layer inside Amazon Linux 2
├── docker-compose.yaml # Local PostgreSQL with pgvector extension
├── env.json # SAM local environment config
├── template.yaml # AWS SAM template defining function and layer
├── Makefile # Helper tasks for build, test, deployGET /Prod/hello: healthcheck endpoint- Executes
SELECT 1on PostgreSQL via Diesel - Async server using
tokio, withspawn_blockingfor Diesel - Connection pooling with
r2d2 - Robust tracing logs for full request lifecycle
./build_libpq_layer.shThis script:
- Uses Docker (Amazon Linux 2) to compile PostgreSQL’s client library (
libpq) - Extracts headers and
.sofiles - Produces a ready-to-use Lambda layer structure under
libpq_layer/ - Zips it as
libpq_layer.zip(optional)
docker-compose up -d- Exposes
postgres://root:password@test-db:5432/test - Includes
pgvectorextension - Waits for readiness with
pg_isready
make sam-buildThis runs sam build with the correct Rust target and environment configuration.
make sam-runThen visit:
http://127.0.0.1:3000/Prod/hello
Expected response:
{ "message": "Hello World with DB!" }On failure, a descriptive error is returned with full logs via tracing.
The /Prod/hello route performs:
- A pooled DB connection
- A
SELECT 1SQL query - Full logging of success/failure with
tokio::timeouthandling
SAM uses a /{proxy+} path in template.yaml:
Path: /{proxy+}
Method: ANYThis allows Warp to handle all routing. Example route in main.rs:
warp::path!("Prod" / "hello")SAM adds the Prod stage automatically — always include it in local or deployed endpoints.
Highlights from Cargo.toml:
warp+warp_lambda— web + Lambda adapterdiesel+r2d2— DB access + connection poolingtokio— async runtimeopenssl— bundled withvendoredfor static compatibilityserde,serde_json— JSON serializationtracing,tracing-subscriber— structured logging
Deploy to AWS with:
sam deploy --guidedFollow prompts to configure the stack name, region, and IAM roles. The Lambda function and the libpq layer will be deployed together.
To stop local Docker containers:
docker-compose downGitHub Actions (ci.yaml) supports:
- Caching for Rust + Docker layers
- libpq layer build
- Lambda binary compilation via
cargo lambda sam buildvalidation
To test locally:
act -P ubuntu-22.04=catthehacker/ubuntu:act-22.04See: ./.github/workflows/ci.yaml
Special thanks to the maintainers of:
cargo install cross --git https://github.com/cross-rs/cross --branch main --force export CROSS_CONTAINER_ENGINE=docker