In our stack, we use many separated lambda functions (each function as an HTTP or WS handler), and each of those functions imports utils that are quite large in terms of size. Those required utils also handle the connection to external entities (cache and database), so that every time we call the function there's an init phase that lasts between 1 and 2 seconds. As different functions have separated runtimes (even though we use the connection to the same database and cache everywhere), the init phase happens in separation for every function. Is this possible (perhaps with lambda layers) to share the "init phase" among many functions if they require the same dependency? For example, based on the js code below:
import utils from "../common"
const db = utils.get_db()
export const handler_A = async (event) => {
// do sth with db here
}
export const handler_B = async (event) => {
// do sth with db here
}
Let's say we have the execution order as follows: handler_A ----> handler_A ----> handler_B
In that case, the second execution for handler_A
should be able to reuse db
from the first execution, but that's not gonna work for the handler_b
.
Is this possible to reuse the "init phase", or in other words, keep the warm start for handler_B if the handler_A was used a couple of seconds before the handler_B? Any help will be highly appreciated!