Non-trivial AWS Lambda / return async closure?

hi -

using lambda_runtime = 0.3.0

i'm trying to create a "my second aws lambda in rust" - and am getting very stuck on how to write the code. something in my mental model must be wrong, but i can't see where.

I have a main function:

#[tokio::main]
async fn main() -> Result<(), Error> {
   let client = KinesisFirehoseClient::new(Region::EuWest2);
    lambda_runtime::run(handler_fn(lambda_handler)).await?;
    Ok(())
}

async fn lambda_handler(req: ApiGatewayRequest, _c: Context) -> Result<ApiGatewayResponse, Error> {
...  // want access to "client" here 
   Ok(...)
}

I'd like to use the "client", initialised in my main function in my lambda handler.. but how to pass it? handlerfn() - part of the AWS library - requires a function reference, and i can't use an async closure apparently.

i thought

#[tokio::main]
async fn main() -> Result<(), Error> {
   let client = KinesisFirehoseClient::new(Region::EuWest2);
    lambda_runtime::run(handler_fn(build_app(client))).await?;
    Ok(())
}

fn build_app(client: KinesisFirehoseClient) -> ???? {
   ??? what here ???
}

the types in handlerfn() are:

impl<F, A, B, Error, Fut> Handler<A, B> for HandlerFn<F>
where
    F: Fn(A, Context) -> Fut,
    Fut: Future<Output = Result<B, Error>> + Send,
    Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>> + fmt::Display,

i really don't understand how this can be so hard - i must be missing something obvious.

Thank you!

James

I did not try to compile it, but I think it should be something like this.

#[tokio::main]
async fn main() -> Result<(), Error> {
   let client = KinesisFirehoseClient::new(Region::EuWest2);
    lambda_runtime::run(handler_fn(move |a, ctx| {
        lambda_handler(client.clone(), a, ctx)
    })).await?;
    Ok(())
}

async fn lambda_handler(client: KinesisFirehoseClient, req: ApiGatewayRequest, _c: Context) -> Result<ApiGatewayResponse, Error> {
   Ok(...)
}

OMG - legend! - it works

thank you so much - now I'll figure out why!

James

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.