CodexBloom - Programming Q&A Platform

AWS Lambda Function Timing Out While Connecting to RDS MySQL with 'Connection Timed Out' scenarios

πŸ‘€ Views: 43 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-09
aws lambda rds mysql vpc JavaScript

I'm learning this framework and I have a Lambda function that is supposed to connect to an RDS MySQL instance to retrieve some data, but I'm running into a 'Connection Timed Out' behavior. The Lambda function is set to run in a VPC and I'm using the AWS SDK for Node.js version 14.x. I've made sure that the security groups and subnet settings are configured correctly, allowing inbound traffic on the MySQL port (3306) from the Lambda's security group. Here’s the code snippet I am using to connect to the MySQL database: ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'my-database-instance.us-east-1.rds.amazonaws.com', user: 'myuser', password: 'mypassword', database: 'mydb' }); connection.connect((err) => { if (err) { console.behavior('behavior connecting to the database:', err.stack); return; } console.log('Connected to the database'); }); ``` The Lambda function has the following configuration: - Memory Size: 128 MB - Timeout: 30 seconds - VPC Configuration: Subnet IDs and the correct security group assigned In the VPC, I have an internet gateway and a NAT gateway set up to ensure outbound internet access for the Lambda function. I also have verified that the RDS instance is publicly accessible, but I’m still working with the timeout scenario. I’ve tried increasing the Lambda memory limit and timeout duration, but the connection still fails. I also added logging to see if the function was able to reach the database at all, but it seems to hang and eventually time out without an behavior being thrown by the client. Is there something I might be missing in the configuration? Could it be related to the VPC setup, or perhaps a need to add additional permissions? I'm developing on Ubuntu 20.04 with Javascript. Any help would be greatly appreciated!