Crate aws_lambda_router

Crate aws_lambda_router 

Source
Expand description

§AWS Lambda Router

A lightweight, Express-like REST API routing framework for AWS Lambda functions behind CloudFront with support for middleware, authentication, and CORS.

§Features

  • Express-like routing with path parameters
  • Middleware support (auth, logging, CORS, etc.)
  • Automatic CORS preflight handling
  • Type-safe request/response handling
  • Path parameter extraction
  • Query string parsing
  • JSON body parsing
  • Error handling with proper HTTP status codes

§Example

use aws_lambda_router::{Router, Request, Response, Context, handler};
use lambda_runtime::Error;
use serde_json::json;

async fn get_user(req: Request, ctx: Context) -> aws_lambda_router::Result<Response> {
    let user_id = req.path_param("userId").unwrap_or(&"unknown".to_string()).clone();
    Ok(Response::ok(json!({
        "userId": user_id,
        "name": "John Doe"
    })))
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let mut router = Router::new();
    router.get("/api/users/:userId", handler!(get_user));
    lambda_runtime::run(router.into_service()).await
}

Re-exports§

pub use cors::CorsConfig;
pub use error::Result;
pub use error::RouterError;
pub use matcher::PathMatcher;
pub use middleware::Middleware;
pub use middleware::Next;
pub use request::Context;
pub use request::Request;
pub use response::Response;
pub use router::Handler;
pub use router::HandlerFn;
pub use router::Router;

Modules§

cors
error
matcher
middleware
request
response
router

Macros§

handler
Helper macro for creating async handlers