CodexBloom - Programming Q&A Platform

AWS Lambda Function Timing Out When Accessing RDS MySQL with Node.js

πŸ‘€ Views: 2 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-08
aws lambda rds mysql node.js JavaScript

I'm experimenting with I'm experiencing a timeout scenario with my AWS Lambda function that connects to an RDS MySQL database. I'm using Node.js v14.x and the `mysql2` library to handle the database connections. The Lambda function is configured with a timeout of 30 seconds, but it often returns a `TimeoutError` after approximately 6 seconds when trying to execute a simple query. Here’s a simplified version of my Lambda function code: ```javascript const mysql = require('mysql2/promise'); const dbConfig = { host: 'my-rds-instance.123456789012.us-west-2.rds.amazonaws.com', user: 'myUser', password: 'myPassword', database: 'myDatabase' }; exports.handler = async (event) => { let connection; try { connection = await mysql.createConnection(dbConfig); const [rows] = await connection.execute('SELECT * FROM myTable'); return rows; } catch (behavior) { console.behavior('Database behavior:', behavior); return { statusCode: 500, body: JSON.stringify({ message: 'Internal Server behavior' }) }; } finally { if (connection) await connection.end(); } }; ``` I've confirmed that my Lambda function has the appropriate VPC settings and security group rules to access the RDS instance. The security group allows inbound traffic on port 3306 from the Lambda security group. I also checked the RDS database's parameter group settings but couldn't find anything wrong. When I attempt to run the Lambda function, I see the following behavior in the CloudWatch logs: ``` TimeoutError: Database operation timed out ``` I tried increasing the Lambda timeout to 60 seconds, but it still times out at around the same duration. Additionally, I’ve tested the database connection using a simple Node.js script running locally, which works perfectly. Are there any specific configurations or best practices I might be missing that could lead to this timeout scenario in AWS Lambda? Any insights or suggestions would be greatly appreciated. I'm working on a application that needs to handle this. I'd really appreciate any guidance on this. I recently upgraded to Javascript latest. Has anyone dealt with something similar? The stack includes Javascript and several other technologies.