Correct answer - "Move the database connection out of the handler" : Here at every Lambda
function execution, the database connnection handler will be created, and then closed. These
connections steps are expensive in terms of time, and thus should be moved out of the
handler
function so that they are kept in the function execution context, and
re-used across function calls. This is what the function should look like in the end:
mysql = mysqlclient.connect()def handler(event, context): data = event['data'] mysql.execute(f"INSERT INTO foo (bar) VALUES (${data});") return
"Upgrade the MySQL instance type" - The bottleneck here is the MySQL connection object, not the
MySQL instance itself.
"Change the runtime to Node.js" - Re-writing the function in another runtime won't improve the
performance.
"Increase the Lambda function RAM" - While this may help speed-up the Lambda function, as
increasing the RAM also increases the CPU allocated to your function, it only makes sense if RAM
or CPU is a critical factor in the Lambda function performance. Here, the connection object is
at fault.
For more information visit