warp-lambda-starter

πŸš€ Overview

This guide covers:


πŸ› οΈ Prerequisites

Install these tools:

brew install rustup awscli docker aws-sam-cli
cargo install cargo-watch       # for hot reloads
brew install act                # optional: test CI locally

πŸ“¦ Clone the Starter

git clone https://github.com/apjames93/warp-lambda-starter.git
cd warp-lambda-starter

You’ll get:


🧱 Project Structure

.
β”œβ”€β”€ backend/                # Rust source
β”œβ”€β”€ aws/                   # SAM template, Docker builds
β”œβ”€β”€ aws/libpq_layer/       # Prebuilt libpq + OpenSSL
β”œβ”€β”€ docker-compose.yaml    # Local Postgres
β”œβ”€β”€ Makefile               # Build/run shortcuts
└── .github/workflows/     # CI pipeline

🐘 Local Postgres with Docker Compose

Run PostgreSQL (pgvector-enabled) locally:

docker-compose up

Access it via:

postgres://root:password@localhost:5001/test

Inside SAM (Docker network sam-local):

"DATABASE_URL": "postgres://root:password@test-db:5432/test?sslmode=disable"

πŸ§ͺ Build the libpq Lambda Layer

Quick start: The aws/libpq_layer/ directory is pre-committed so you can start building and deploying immediately.

Advanced: If you want to regenerate the layer (e.g. for a different PostgreSQL version, security updates, or reduced size), run:

make aws-docker-sh-libpq

This command:

You’re free to customize or rebuild the layer anytimeβ€”just modify aws/docker/build_libpq_layer_docker.sh.

Rebuild if:


🧰 Build the Statically Linked Rust Binary

Compile for Lambda using a dedicated Dockerfile:

make aws-build-sam

Set in Docker:

RUSTFLAGS="-L /aws/libpq_layer/lib \
  -C link-arg=-lpq -C link-arg=-lssl -C link-arg=-lcrypto \
  -C link-arg=-lz -C link-arg=-static"

πŸ”§ Local Dev vs Lambda Mode

Your main.rs supports both:

#[cfg(feature = "lambda")]
warp_lambda::run(service).await?;

#[cfg(not(feature = "lambda"))]
warp::serve(routes).run(([0, 0, 0, 0], 3000)).await;

In Cargo.toml:

[features]
default = ["lambda"]
lambda = ["lambda_http", "lambda_runtime"]

So you can:


If you want to clean up your stack after testing or deployment:

make aws-delete-sam

This deletes the deployed stack using sam delete –no-prompts


πŸ”— Why the Lambda Layer Is Still Needed

Even with static linking, Lambda may expect .so files at runtime:

To ensure compatibility, we attach the same static artifacts as a Lambda Layer:

Layers:
  - !Ref LibpqLayer
Environment:
  Variables:
    LD_LIBRARY_PATH: /opt/lib
    PQ_LIB_DIR: /opt/lib
    PQ_INCLUDE_DIR: /opt/include/libpq

🧠 Lightbulb moment: Build-time and runtime use the exact same files. No duplication. No surprises.


πŸ”„ Local Dev with Hot Reloads

Run the API locally with:

make run-backend

This:

Great for development without SAM overhead.


🌐 Test Locally with SAM

Test your Lambda function locally via Docker:

make aws-run-sam

Visit:

http://localhost:4040/Prod/hello

☁️ Deploy to AWS

Deploy your app with:

make aws-deploy-sam

This runs sam deploy and provisions:


πŸ”„ CI/CD Pipeline

GitHub Actions CI runs on push to main:

Test locally with:

act push \
  -W .github/workflows/ci.yaml \
  --secret-file .env \
  -P ubuntu-22.04=catthehacker/ubuntu:act-22.04

βœ… Summary

Why this setup works:


🧭 Next Steps (Coming Soon)


πŸ¦€ A solid foundation for production-ready, serverless Rust APIs.

Happy building!