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
tracing
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

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
Starts the Lambda Rust runtime and stream response back Configure Lambda Streaming Response.
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