What is the best way to handle connections to Redis & other data access providers within AWS Lambda spanning across many warm start invocations?
I'm getting the following error from Redis Error: Connection timeout. I believe this is from the Lambda being 'warm' for too long, causing the Redis connection time out for the next & all subsequent invocations until the Lambda goes cold
Here's the configuration I'm using:
const client = createClient({
// creation configuration,
});
const connection = client.connect();
export const handler = async () => {
try {
await connection;
// do things with client
} catch (err) {
await client.disconnect();
}
};
A bit hesitant to connect within the handler as it omits the benefits of warm starts
Appreciate any advice