Skip to content

How do I troubleshoot connection timeout errors from Lambda when I access an Amazon RDS database instance?

4 minute read
1

My AWS Lambda function receives connection timeout errors when it tries to access an Amazon Relational Database Service (Amazon RDS) database instance.

Resolution

Troubleshoot intermittent and transient connection timeout errors

Capture the Lambda function's events and retry them as part of your function's error handling

For more information, see Understanding retry behavior in Lambda.

Check for DNS errors

Confirm whether there are Domain Name System (DNS)-related errors. If there are DNS errors, then take note of the errors. Then, use an exponential backoff to retry the DNS request. For more information, see Retry behavior.

Note: Make sure that your Amazon Virtual Private Cloud (Amazon VPC) has DNS resolution activated. On the Amazon VPC console, find the VPC for your Lambda function and Amazon RDS database instance. Then, make sure that you set the DNS attributes enableDnsSupport and enableDnsHostnames to true.

Troubleshoot consistent connection timeout errors

Check the Lambda function's Amazon CloudWatch Logs

Make sure that you grant CloudWatch logging permissions to your Lambda function. Then, review your function's logs to get specific connection timeout errors.

For more information, see Monitoring, debugging, and troubleshooting Lambda functions.

Check the VPC configuration

For private databases, do either of the following, based on your configuration:

Put your Lambda function in the same Amazon VPC as the database.

-or-

Create a VPC peering connection between the Amazon VPC that includes your function and the Amazon VPC that includes your database.

For public databases, do the following:

For functions in an Amazon VPC, route public traffic through a network address translation (NAT) gateway or NAT instance. For more information, see How do I give internet access to my Lambda function that's connected to an Amazon VPC?

Note: If the database is publicly accessible and there are no IP address-based restrictions on the database, then check your configuration requirements. Follow the instructions in Tutorial: Using a Lambda function to access an Amazon RDS database.

Check the security group rules

For private databases, add an inbound rule to your database security group. Make sure that the database security group allows traffic to the database's port from the CIDR range of the VPC's subnets or security group.

For public databases, add an inbound rule to your database security group that allows traffic from the NAT gateway or NAT instance Elastic IP address.

Note: Security groups are stateful. When a security group blocks traffic to the database port, the connection fails silently and doesn't return errors.

Update the Lambda function handler

Update your Lambda function code so that the function opens the database connection outside of the function handler and doesn't close the connection.

Note: Lambda reuses execution environments for subsequent invocations. Code that's outside of the function handler only runs during the cold start and persists across warm invocations. It's a best practice to create the database connection outside the handler so that it's reused on subsequent invocations. Make sure that you confirm that the database connection is valid before you use it. If the connection is stale or closed, then create a new connection.

Use an Amazon EC2 instance to connect

As a test, launch an Amazon Elastic Compute Cloud (Amazon EC2) instance with the same Amazon VPC configuration as your Lambda function. Then, connect to the Amazon RDS database instance from the Amazon EC2 instance.

If you can connect to the database, then follow the instructions in Troubleshoot networking issues in Lambda.

If you can't connect to the database, then see How do I troubleshoot issues when I connect to my Amazon RDS database instance?

Use Amazon RDS Proxy

Create an Amazon RDS Proxy endpoint for your RDS database to handle the database traffic from your Lambda function. For more information, see Using Amazon RDS Proxy with AWS Lambda.

Note: For a list of supported database engines and AWS Regions for Amazon RDS Proxy, see Supported Regions and database engines for Amazon RDS Proxy.

Check your Lambda function timeout limit

Timeout errors can occur when your Lambda function code runs longer than the function's service quota allows. For example, if your function duration is 10 seconds but your timeout limit is set to 5 seconds, then you get a timeout error. To resolve this issue, increase your Lambda function timeout limit.

Related information

Using AWS Lambda with Amazon RDS

How do I connect to an Amazon RDS database instance that uses a public or private subnet of an Amazon VPC?

AWS OFFICIALUpdated 4 months ago