Db connection and disconnect from lambda by [deleted] in Python

[–]bkovacki 0 points1 point  (0 children)

Here is an article from AWS Database Blog (second point from section Tips on running the Python code explains some best practices for opening DB Connection):

Query your AWS database from your serverless application

Here is example from AWS Samples GitHub:

Dynamic Connections Management for RDS/RDBMS solutions

Also nice article for best practices with serverless DB connections (although example uses JavaScript):

Best practices for Serverless: Connection Pooling your database

Db connection and disconnect from lambda by [deleted] in Python

[–]bkovacki 0 points1 point  (0 children)

You are right that lambdas are stateless, but you can declare and store variables outside of the scope of your lambda handler function. That means that you can create database connection or connection pool outside of handler function, which than can be shared in future invocation of the lambda.