pub struct Adapter<C, B> { /* private fields */ }Expand description
The Lambda Web Adapter.
This is the main struct that handles forwarding Lambda events to your web application.
It implements the tower::Service trait, allowing it to be used with the Lambda runtime.
§Type Parameters
C- The HTTP connector type (typicallyhyper_util::client::legacy::connect::HttpConnector)B- The request body type (typicallylambda_http::Body)
§Lifecycle
- Create an adapter with
Adapter::new() - Register as a Lambda extension with
Adapter::register_default_extension() - Wait for the web app to be ready with
Adapter::check_init_health() - Start processing events with
Adapter::run()
§Examples
use lambda_web_adapter::{Adapter, AdapterOptions};
let options = AdapterOptions::default();
let mut adapter = Adapter::new(&options)?;
adapter.register_default_extension();
adapter.check_init_health().await;
adapter.run().awaitImplementations§
Source§impl Adapter<HttpConnector, Body>
impl Adapter<HttpConnector, Body>
Sourcepub fn new(
options: &AdapterOptions,
) -> Result<Adapter<HttpConnector, Body>, Error>
pub fn new( options: &AdapterOptions, ) -> Result<Adapter<HttpConnector, Body>, Error>
Creates a new HTTP Adapter instance.
This function initializes a new HTTP client configured to communicate with your web application. The client uses connection pooling with a 4-second idle timeout for optimal Lambda performance.
§Arguments
options- Configuration options for the adapter
§Returns
Returns Ok(Adapter) on success, or an error if the configuration is invalid.
§Errors
Returns an error if:
- The configured host, port, or readiness check path contain invalid URL characters
- TCP protocol is configured but the URL is missing host or port
§Examples
use lambda_web_adapter::{Adapter, AdapterOptions};
let options = AdapterOptions::default();
let adapter = Adapter::new(&options).expect("Failed to create adapter");Source§impl Adapter<HttpConnector, Body>
impl Adapter<HttpConnector, Body>
Sourcepub fn register_default_extension(&self)
pub fn register_default_extension(&self)
Registers the adapter as a Lambda extension.
Lambda extensions are loaded before the function handler and can perform initialization tasks. This registration ensures the adapter is ready to receive events before your function starts processing.
The registration happens asynchronously in a background task. If registration fails, the process will exit with code 1 to signal Lambda that initialization failed.
§Panics
This method spawns a background task that will call std::process::exit(1)
if extension registration fails, terminating the Lambda execution environment.
Sourcepub async fn check_init_health(&mut self)
pub async fn check_init_health(&mut self)
Checks if the web application is ready during Lambda initialization.
This method performs readiness checks against your web application using the configured protocol (HTTP or TCP) and endpoint.
§Async Initialization
If async_init is enabled in the adapter options, this method will:
- Attempt readiness checks for up to 9.8 seconds
- Return early if the timeout is reached (to avoid Lambda’s 10s init timeout)
- Allow the application to continue booting in the background
The first request will re-check readiness if the application wasn’t ready during initialization.
§Examples
use lambda_web_adapter::{Adapter, AdapterOptions};
let options = AdapterOptions::default();
let mut adapter = Adapter::new(&options)?;
adapter.check_init_health().await;Sourcepub async fn run(self) -> Result<(), Error>
pub async fn run(self) -> Result<(), Error>
Starts the adapter and begins processing Lambda events.
This method blocks and runs the Lambda runtime loop, receiving events and forwarding them to your web application.
§Safety
If AWS_LWA_LAMBDA_RUNTIME_API_PROXY is set, Adapter::apply_runtime_proxy_config()
must be called BEFORE starting the tokio runtime to avoid race conditions.
§Returns
Returns Ok(()) when the Lambda runtime shuts down gracefully, or an error
if there’s a fatal issue with the runtime.
§Examples
use lambda_web_adapter::{Adapter, AdapterOptions};
let options = AdapterOptions::default();
let adapter = Adapter::new(&options)?;
adapter.run().awaitSourcepub fn apply_runtime_proxy_config()
pub fn apply_runtime_proxy_config()
Applies runtime API proxy configuration from environment variables.
If AWS_LWA_LAMBDA_RUNTIME_API_PROXY is set, this method overwrites
AWS_LAMBDA_RUNTIME_API to redirect Lambda runtime calls through the proxy.
§Important
This method must be called before starting the tokio runtime to avoid race conditions with environment variable modification in a multi-threaded context.
§Safety Note
This function uses std::env::set_var which modifies process-wide state.
In future Rust versions, this will be marked unsafe due to potential race
conditions. Calling this before spawning any threads ensures safety.
§Examples
use lambda_web_adapter::Adapter;
fn main() {
// Call before starting tokio runtime
Adapter::apply_runtime_proxy_config();
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
runtime.block_on(async {
// ... adapter setup and run
});
}Trait Implementations§
Source§impl Service<Request<Body>> for Adapter<HttpConnector, Body>
Implementation of tower::Service for the adapter.
impl Service<Request<Body>> for Adapter<HttpConnector, Body>
Implementation of tower::Service for the adapter.
This allows the adapter to be used directly with the Lambda runtime,
which expects a Service that can handle Lambda events.
Source§type Future = Pin<Box<dyn Future<Output = Result<<Adapter<HttpConnector, Body> as Service<Request<Body>>>::Response, <Adapter<HttpConnector, Body> as Service<Request<Body>>>::Error>> + Send>>
type Future = Pin<Box<dyn Future<Output = Result<<Adapter<HttpConnector, Body> as Service<Request<Body>>>::Response, <Adapter<HttpConnector, Body> as Service<Request<Body>>>::Error>> + Send>>
Auto Trait Implementations§
impl<C, B> Freeze for Adapter<C, B>
impl<C, B> !RefUnwindSafe for Adapter<C, B>
impl<C, B> Send for Adapter<C, B>
impl<C, B> Sync for Adapter<C, B>
impl<C, B> Unpin for Adapter<C, B>
impl<C, B> UnsafeUnpin for Adapter<C, B>
impl<C, B> !UnwindSafe for Adapter<C, B>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
Source§fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
Service, calling it with the provided request once it is ready.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
poll_ready method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
poll_ready method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
poll_ready method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
Result<Self::Response, Self::Error>)
to a different value, regardless of whether the future succeeds or
fails. Read more