Background
I'm building a Lambda function (Node.js 20 runtime) that connects to an Amazon RDS MySQL database within the same VPC.
Both the Lambda and the RDS instance are deployed in private subnets.
However, every attempt to connect results in a connection timeout.
What I’ve tried
- Verified that the Lambda is in the same VPC and private subnets as the RDS instance.
- Allowed inbound traffic on port 3306 from the Lambda's security group to the RDS security group.
- Added a NAT Gateway to the subnets used by the Lambda (no change).
- Tested connectivity from an EC2 instance in the same subnet — connection succeeds.
Code sample
import mysql from "mysql2/promise";
export const handler = async () => {
const conn = await mysql.createConnection({
host: process.env.DB_HOST,
user: "admin",
password: process.env.DB_PASS,
});
const [rows] = await conn.query("SELECT 1");
console.log(rows);
};
Question
What could cause a Lambda function inside a VPC to time out when connecting to an RDS instance in the same VPC, even though security groups and subnets seem properly configured?