CodexBloom - Programming Q&A Platform

AWS Lambda with API Gateway Timing Out When Retrieving Data from RDS MySQL

πŸ‘€ Views: 19 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-13
aws lambda api-gateway mysql rds JavaScript

I'm relatively new to this, so bear with me... Quick question that's been bugging me - I'm working on a project and hit a roadblock... After trying multiple solutions online, I still can't figure this out. I'm working with an scenario where my AWS Lambda function, which is triggered by an API Gateway, times out when trying to query data from an RDS MySQL database. I've set the timeout for the Lambda function to 30 seconds, but I'm still running into a `Task timed out after 30.00 seconds` behavior when executing the query. The Lambda function works perfectly when I run it locally, but the performance drops significantly in the AWS environment. Here’s the relevant snippet of my function: ```javascript const mysql = require('mysql2/promise'); const connectionConfig = { host: 'mydbinstance.abcdefghijk.us-east-1.rds.amazonaws.com', user: 'myusername', password: 'mypassword', database: 'mydatabase', }; exports.handler = async (event) => { const connection = await mysql.createConnection(connectionConfig); const [rows] = await connection.execute('SELECT * FROM mytable WHERE id = ?', [event.id]); return rows; }; ``` I've also tried changing the connection pooling settings, increasing the memory allocated to the Lambda function, and setting the RDS instance to a higher class, but nothing seems to help. I suspect it might be an scenario with VPC configuration as the Lambda function is inside a VPC and the RDS instance is also in the same VPC. I've ensured that the security groups allow inbound traffic on the MySQL port, but still, the function times out frequently. Additionally, I've checked the CloudWatch logs, and there are no specific errors apart from the timeout message. I’m using the `mysql2` package version 2.3.3 and the Lambda function runs in Node.js runtime 14.x. Any insights on how to resolve this would be greatly appreciated! For context: I'm using Javascript on Linux. How would you solve this? The project is a service built with Javascript. Thanks for your help in advance! I recently upgraded to Javascript latest. Any feedback is welcome!