I am trying to create custom context that gets reused between invocations. Here specifically trying to reuse mongodb client. ``` // ...initing mongo database let custom_context = CustomContext { mongo_database: Some(mongo_database), }; lambda_runtime::run(handler_fn(|event: Request, ctx: Context| { execute(event, ctx, &custom_context) })) ``` Getting: Closure may outlive the current function, but it borrows `custom_context`, which is owned by the current function Can `handler_fn` be used to support something like this?
Activity
calavera commentedon Apr 3, 2022
Yes, See this example that shares custom resources: https://github.com/awslabs/aws-lambda-rust-runtime/blob/main/lambda-http/examples/shared-resources-example.rs
Your current code is bumping into Rust borrowing challenges. There is a good explanation on how it works here: https://blog.logrocket.com/introducing-the-rust-borrow-checker/