Crate fishrock_lambda_http

Crate fishrock_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 optimiztions 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_runtime::run in combination with the handler 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::{handler, lambda_runtime::{self, Error}};

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

§Leveraging trigger provided data

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

use lambda_http::{handler, lambda_runtime::{self, Context, Error}, IntoResponse, Request, RequestExt};

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

async fn hello(
    request: Request,
    _: Context
) -> Result<impl IntoResponse, Error> {
    Ok(format!(
        "hello {}",
        request
            .query_string_parameters()
            .get("name")
            .unwrap_or_else(|| "stranger")
    ))
}

Re-exports§

pub use crate::ext::RequestExt;
pub use http;
pub use lambda_runtime;

Modules§

ext
Extension methods for http::Request types
request
ALB and API Gateway request adaptations

Structs§

Adapter
Exists only to satisfy the trait cover rule for lambda_runtime::Handler impl
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.
Response
Represents an HTTP response
StrMap
A read-only view into a map of string data which may contain multiple values

Enums§

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

Traits§

Handler
Functions serving as ALB and API Gateway REST and HTTP API handlers must conform to this type.
IntoResponse
A conversion of self into a Response<Body> for various types.

Functions§

handler
Adapts a Handler to the lambda_runtime::run interface

Type Aliases§

Request
Type alias for http::Requests with a fixed Body type