Unable to connect to RDS during local development with sls offline

I’m facing an issue during local development with sls offline. I’m unable to connect to my RDS instance and keep getting this error: KnexTimeoutError: Timeout acquiring a connection. Has anyone else run into this? Is there a way to work around it or simulate the RDS connection locally so I can continue working with sls offline?

1 Like

@Suprith

I faced a similar issue during development and found a way to fix it.

When running sls offline, your Lambda functions attempt to connect to the RDS instance using its AWS-hosted endpoint. However, since this endpoint isn’t accessible from your local machine, the connection fails — resulting in the following error:


KnexTimeoutError: Knex: Timeout acquiring a connection


Use a Local PgAdmin for Offline Development

You can configure your project to connect to a local database while using sls offline, and use the real RDS endpoint when deploying to AWS.

1. Update serverless.yml


environment:

RDS_CLUSTER_HOST: ${env:RDHOST, ssm:RDS_CLUSTER_HOST}

This setup checks:

  • If the RDHOST environment variable is set (used for local development).

  • Falls back to the SSM parameter (RDS_CLUSTER_HOST) in deployed environments.


2. Start Offline with Localhost as Host

Run sls offline like this:


RDHOST=127.0.0.1 sls offline --stage dev --region me-central-1

This tells your Lambda code to connect to a locally running DB (e.g., PostgreSQL or MySQL) instead of the unreachable RDS endpoint.


3. Deploy Normally for AWS Environments

For staging or production, just deploy as usual:


sls deploy --stage prod --region me-central-1

In this case, the real RDS host will be pulled from the SSM parameter store, and your Lambda will work as expected in the AWS environment.


6 Likes