Skip to main content

runtime

Function runtime 

Source
pub fn runtime<R, S, E>(
    handler: S,
) -> Runtime<impl Service<LambdaInvocation, Response = (), Error = Error>>
where S: Service<Request, Response = R, Error = E> + Send + 'static, S::Future: Send + 'static, R: IntoResponse + Send + Sync + 'static, E: Debug + Into<Diagnostic> + Send + 'static,
Expand description

Returns a configured Runtime wrapping the given handler, without starting the event loop.

Use this when you need to register SnapStartResources for the SnapStart snapshot/restore lifecycle. For the common case where no custom hooks are needed, use run() instead — SnapStart support works automatically.

§Example

use lambda_http::{service_fn, BoxFuture, Error, Request, SnapStartResource};
use std::sync::Arc;

struct Pool;
impl SnapStartResource for Pool {
    fn before_snapshot(&self) -> BoxFuture<'_, Result<(), Error>> {
        Box::pin(async move { Ok(()) })
    }
    fn after_restore(&self) -> BoxFuture<'_, Result<(), Error>> {
        Box::pin(async move { Ok(()) })
    }
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let pool = Arc::new(Pool);
    let runtime = lambda_http::runtime(service_fn(handler))
        .register_snapstart_resource(pool.clone());

    runtime.run().await?;
    Ok(())
}

async fn handler(_req: Request) -> Result<&'static str, std::convert::Infallible> {
    Ok("hello")
}

§Panics

This function panics if required Lambda environment variables are missing (AWS_LAMBDA_FUNCTION_NAME, AWS_LAMBDA_FUNCTION_MEMORY_SIZE, AWS_LAMBDA_FUNCTION_VERSION, AWS_LAMBDA_RUNTIME_API).