Crate lambda_http

Crate lambda_http 

Source
Expand description

Enriches the lambda crate with http types targeting AWS ALB, API Gateway REST and HTTP API lambda integrations.

This crate abstracts over all of these trigger events using standard http types minimizing the mental overhead of understanding the nuances and variation between trigger details allowing you to focus more on your application while also giving you to the maximum flexibility to transparently use whichever lambda trigger suits your application and cost optimizations best.

§Examples

§Hello World

The following example is how you would structure your Lambda such that you have a main function where you explicitly invoke lambda_http::run in combination with the service_fn function. This pattern allows you to utilize global initialization of tools such as loggers, to use on warm invokes to the same Lambda function after the first request, helping to reduce the latency of your function’s execution path.

use lambda_http::{service_fn, Error};

#[tokio::main]
async fn main() -> Result<(), Error> {
    // initialize dependencies once here for the lifetime of your
    // lambda task
    lambda_http::run(service_fn(|request| async {
        Result::<&str, std::convert::Infallible>::Ok("👋 world!")
    })).await?;
    Ok(())
}

§Leveraging trigger provided data

You can also access information provided directly from the underlying trigger events, like query string parameters, or Lambda function context, with the RequestExt trait.

use lambda_http::{service_fn, Error, RequestExt, IntoResponse, Request};

#[tokio::main]
async fn main() -> Result<(), Error> {
    lambda_http::run(service_fn(hello)).await?;
    Ok(())
}

async fn hello(
    request: Request
) -> Result<impl IntoResponse, std::convert::Infallible> {
    let _context = request.lambda_context_ref();

    Ok(format!(
        "hello {}",
        request
            .query_string_parameters_ref()
            .and_then(|params| params.first("name"))
            .unwrap_or_else(|| "stranger")
    ))
}

Re-exports§

pub use crate::ext::RequestExt;
pub use crate::ext::RequestPayloadExt;
pub use http;
pub use lambda_runtime;
pub use lambda_runtime::tower;
pub use aws_lambda_events;

Modules§

ext
Extension methods for Request types
request
ALB and API Gateway request adaptations
tracingtracing
Utilities to initialize and use tracing and tracing-subscriber in Lambda Functions. This module provides primitives to work with tracing and tracing-subscriber in Lambda functions.

Structs§

Context
The Lambda function execution context. The values in this struct are populated using the Lambda environment variables and the headers returned by the poll request to the Runtime APIs.
LambdaEvent
Incoming Lambda request containing the event payload and context.
Response
Represents an HTTP response
StreamAdapter
An adapter that lifts a standard Service<Request> into a Service<LambdaEvent<LambdaRequest>> which produces streaming Lambda HTTP responses.

Enums§

Body
Representation of http request and response bodies as supported by API Gateway and ALBs.

Traits§

IntoResponse
Trait for generating responses
Service
An asynchronous function from a Request to a Response.

Functions§

run
Starts the Lambda Rust runtime and begins polling for events on the Lambda Runtime APIs.
run_with_streaming_response
Runs the Lambda runtime with a handler that returns streaming HTTP responses.
service_fn
Returns a new ServiceFn with the given closure.

Type Aliases§

Error
Error type that lambdas may result in
Request
Type alias for http::Requests with a fixed Body type
","ArrayChunks":"

Notable traits for ArrayChunks<I, N>

impl<I, const N: usize> Iterator for ArrayChunks<I, N>
where\n I: Iterator,
type Item = [<I as Iterator>::Item; N];
","Bytes":"

Notable traits for Bytes<R>

impl<R> Iterator for Bytes<R>
where\n R: Read,
type Item = Result<u8, Error>;
","Chain::IntoIter>":"

Notable traits for Chain<A, B>

impl<A, B> Iterator for Chain<A, B>
where\n A: Iterator,\n B: Iterator<Item = <A as Iterator>::Item>,
type Item = <A as Iterator>::Item;
","Cloned":"

Notable traits for Cloned<I>

impl<'a, I, T> Iterator for Cloned<I>
where\n T: 'a + Clone,\n I: Iterator<Item = &'a T>,
type Item = T;
","Copied":"

Notable traits for Copied<I>

impl<'a, I, T> Iterator for Copied<I>
where\n T: 'a + Copy,\n I: Iterator<Item = &'a T>,
type Item = T;
","Cycle":"

Notable traits for Cycle<I>

impl<I> Iterator for Cycle<I>
where\n I: Clone + Iterator,
type Item = <I as Iterator>::Item;
","EitherWriter":"

Notable traits for EitherWriter<A, B>

impl<A, B> Write for EitherWriter<A, B>
where\n A: Write,\n B: Write,
","EitherWriter":"

Notable traits for EitherWriter<A, B>

impl<A, B> Write for EitherWriter<A, B>
where\n A: Write,\n B: Write,
","Enumerate":"

Notable traits for Enumerate<I>

impl<I> Iterator for Enumerate<I>
where\n I: Iterator,
type Item = (usize, <I as Iterator>::Item);
","Filter":"

Notable traits for Filter<I, P>

impl<I, P> Iterator for Filter<I, P>
where\n I: Iterator,\n P: FnMut(&<I as Iterator>::Item) -> bool,
type Item = <I as Iterator>::Item;
","FilterMap":"

Notable traits for FilterMap<I, F>

impl<B, I, F> Iterator for FilterMap<I, F>
where\n I: Iterator,\n F: FnMut(<I as Iterator>::Item) -> Option<B>,
type Item = B;
","FlatMap":"

Notable traits for FlatMap<I, U, F>

impl<I, U, F> Iterator for FlatMap<I, U, F>
where\n I: Iterator,\n U: IntoIterator,\n F: FnMut(<I as Iterator>::Item) -> U,
type Item = <U as IntoIterator>::Item;
","Flatten":"

Notable traits for Flatten<I>

impl<I, U> Iterator for Flatten<I>
where\n I: Iterator,\n <I as Iterator>::Item: IntoIterator<IntoIter = U, Item = <U as Iterator>::Item>,\n U: Iterator,
type Item = <U as Iterator>::Item;
","Fuse":"

Notable traits for Fuse<I>

impl<I> Iterator for Fuse<I>
where\n I: Iterator,
type Item = <I as Iterator>::Item;
","Inspect":"

Notable traits for Inspect<I, F>

impl<I, F> Iterator for Inspect<I, F>
where\n I: Iterator,\n F: FnMut(&<I as Iterator>::Item),
type Item = <I as Iterator>::Item;
","Intersperse":"

Notable traits for Intersperse<I>

impl<I> Iterator for Intersperse<I>
where\n I: Iterator,\n <I as Iterator>::Item: Clone,
type Item = <I as Iterator>::Item;
","IntersperseWith":"

Notable traits for IntersperseWith<I, G>

impl<I, G> Iterator for IntersperseWith<I, G>
where\n I: Iterator,\n G: FnMut() -> <I as Iterator>::Item,
type Item = <I as Iterator>::Item;
","Lines":"

Notable traits for Lines<B>

impl<B> Iterator for Lines<B>
where\n B: BufRead,
type Item = Result<String, Error>;
","Map":"

Notable traits for Map<I, F>

impl<B, I, F> Iterator for Map<I, F>
where\n I: Iterator,\n F: FnMut(<I as Iterator>::Item) -> B,
type Item = B;
","MapWhile":"

Notable traits for MapWhile<I, P>

impl<B, I, P> Iterator for MapWhile<I, P>
where\n I: Iterator,\n P: FnMut(<I as Iterator>::Item) -> Option<B>,
type Item = B;
","MapWindows":"

Notable traits for MapWindows<I, F, N>

impl<I, F, R, const N: usize> Iterator for MapWindows<I, F, N>
where\n I: Iterator,\n F: FnMut(&[<I as Iterator>::Item; N]) -> R,
type Item = R;
","Peekable":"

Notable traits for Peekable<I>

impl<I> Iterator for Peekable<I>
where\n I: Iterator,
type Item = <I as Iterator>::Item;
","Rev":"

Notable traits for Rev<I>

impl<I> Iterator for Rev<I>
type Item = <I as Iterator>::Item;
","Scan":"

Notable traits for Scan<I, St, F>

impl<B, I, St, F> Iterator for Scan<I, St, F>
where\n I: Iterator,\n F: FnMut(&mut St, <I as Iterator>::Item) -> Option<B>,
type Item = B;
","Skip":"

Notable traits for Skip<I>

impl<I> Iterator for Skip<I>
where\n I: Iterator,
type Item = <I as Iterator>::Item;
","SkipWhile":"

Notable traits for SkipWhile<I, P>

impl<I, P> Iterator for SkipWhile<I, P>
where\n I: Iterator,\n P: FnMut(&<I as Iterator>::Item) -> bool,
type Item = <I as Iterator>::Item;
","Split":"

Notable traits for Split<B>

impl<B> Iterator for Split<B>
where\n B: BufRead,
type Item = Result<Vec<u8>, Error>;
","StepBy":"

Notable traits for StepBy<I>

impl<I> Iterator for StepBy<I>
where\n I: Iterator,
type Item = <I as Iterator>::Item;
","Take":"

Notable traits for Take<I>

impl<I> Iterator for Take<I>
where\n I: Iterator,
type Item = <I as Iterator>::Item;
","TakeWhile":"

Notable traits for TakeWhile<I, P>

impl<I, P> Iterator for TakeWhile<I, P>
where\n I: Iterator,\n P: FnMut(&<I as Iterator>::Item) -> bool,
type Item = <I as Iterator>::Item;
","Zip::IntoIter>":"

Notable traits for Zip<A, B>

impl<A, B> Iterator for Zip<A, B>
where\n A: Iterator,\n B: Iterator,
type Item = (<A as Iterator>::Item, <B as Iterator>::Item);
"}