AWS Lambda is a lightweight compute service: pay for what you use and let AWS handle packaging and scaling.
Billing tracks duration and memory, which is where Rust tends to shine—hence all the serverless handlers written in Rust.
Below: cargo-lambda for a Rust handler, local watch, and a LocalStack deploy—you don’t need a real AWS account for the sandbox at the end.
cargo-lambda
cargo-lambda is the usual tool for building Lambda functions (AWS points people at it too).
Install:
1 | brew tap cargo-lambda/cargo-lambda |
Scaffold a demo:
1 | cargo lambda new lambda-demo |
The template pulls in lambda-http and tokio for the runtime.
Handler sketch—a hello-world HTTP response:
1 | use lambda_http::{run, service_fn, tracing, Body, Error, Request, RequestExt, Response}; |
Side note: service_fn look familiar? The handler sits on the same Service trait shape as tower—because this stack uses hyper, the usual bedrock for Rust HTTP.
Run locally
During development, watch rebuilds on change and serves invocations locally:
1 | cargo lambda watch |
Invoke from the CLI:
1 | cargo lambda invoke lambda-demo --data-ascii "?name=newbmiao" |
Build artifact
1 | cargo lambda build --release --arm64 |
Output lands at ./target/lambda/lambda-demo/bootstrap (for a workspace, check the workspace root target tree).
Zip the binary for upload:
1 | zip bootstrap.zip bootstrap |
Deploy
Deployment is straightforward; the friction is often “I do not have AWS credentials.” LocalStack emulates AWS on your machine.
docker-compose up for LocalStack:
1 | version: "3.8" |
Terraform for the Lambda itself:
1 | # build |
IAM plus function URL:
1 | resource "aws_lambda_function_url" "lambda_demo_url" { |
After terraform apply, grab the URL from outputs, e.g.:
1 | ... |
End-to-end sample: lambda-demo (build.sh walks through packaging).
Questions? Leave a comment below or email: newbvirgil@gmail.com
Permalink: https://newbmiao.github.io/rust-lambda.html
