Connecting Applications to Relational Databases Efficiently and Securely
This lesson explains how applications connect to relational databases efficiently, through the right endpoints, and without exposing credentials.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why Lambda functions can exhaust database connections and how Amazon RDS Proxy prevents it.
- Choose the right Aurora endpoint for writes and reads so the application keeps working through failover.
- Choose between AWS Secrets Manager, IAM database authentication and other options for protecting database credentials.
Managing connections from Lambda
Each Lambda execution environment opens its own database connection, so a function scaled to 500 concurrent executions can open 500 connections, more than many database instances allow. When connections run out, new invocations fail with connection errors even though the database has spare CPU. Creating the connection outside the handler means each environment reuses one connection across invocations, but it does not limit how many environments exist. Two services address the problem.
RDS Proxy sits between the functions and the database. Functions connect to the proxy, which keeps a smaller pool of connections to the database and shares them among clients, so a burst of invocations does not exhaust the database. During a failover, the proxy keeps client connections open and redirects them to the new primary, which shortens the interruption. It reads the database credentials from Secrets Manager and can require IAM authentication from clients, so functions hold no passwords.
The RDS Data API, available for Aurora, removes connections from the function altogether. The function sends each SQL statement as an HTTPS request, authorised by IAM and a Secrets Manager secret, and needs no database driver or VPC configuration. It suits functions that run occasional short queries. The following table summarises the two options.
| Option | What it does |
|---|---|
| RDS Proxy | Pools and shares connections, shortens failover time, and can authenticate with IAM and Secrets Manager |
| RDS Data API (Aurora) | Runs SQL over HTTPS with IAM and Secrets Manager credentials, with no drivers or persistent connections |
The following diagram compares functions connecting directly to a database with connecting through RDS Proxy.

Using the right endpoints
Amazon RDS and Aurora provide DNS endpoints, and choosing the correct one determines whether the application keeps working through failures. An Aurora cluster has a cluster endpoint, also called the writer endpoint, which always points to the current primary instance, and a reader endpoint, which spreads connections across the Aurora Replicas. Writes go to the cluster endpoint and reads that can tolerate slight replication delay go to the reader endpoint, which moves read load off the primary.
Each instance also has its own instance endpoint. Applications should not hard-code these, because during a failover a replica becomes the new primary: the cluster endpoint is updated to point to it, but an application connected to the old primary's instance endpoint would keep sending writes to an instance that can no longer accept them. For RDS Multi-AZ DB instances, the single DB instance endpoint is likewise updated automatically during failover. Applications should also keep DNS caching short and reconnect after a dropped connection, so that they find the new primary quickly.
Protecting credentials
Database credentials must be stored and retrieved securely. The following list describes the available options.
- Secrets Manager: store credentials with automatic rotation, and cache them so the application does not call Secrets Manager on every request.
- IAM database authentication (MySQL and PostgreSQL): the application generates a short-lived token instead of using a password.
- Network: keep databases in private subnets, with security groups that admit only the application, and connect over TLS.
Example. The function generates a token valid for 15 minutes and connects over TLS. Its execution role needs rds-db:connect on the database user.
import boto3, pymysql
host = "orders.cluster-abc123.eu-west-1.rds.amazonaws.com"
token = boto3.client("rds").generate_db_auth_token(DBHostname=host, Port=3306, DBUsername="app_user")
conn = pymysql.connect(
host=host, user="app_user", password=token, database="orders",
ssl={"ca": "/opt/global-bundle.pem"},
)Connect Lambda to Aurora safely
Too many connections from Lambda
A Lambda API connected to Aurora MySQL works in testing. Under load, requests fail with "Too many connections". Each concurrent function environment opens its own connection, and 600 concurrent functions exceed the database's limit.
| Fix | Effect |
|---|---|
| Create the connection outside the handler | Reused across invocations of the same environment |
| Put RDS Proxy between the functions and Aurora | Pools and shares a small number of database connections |
| Use IAM authentication to the proxy and Secrets Manager for the database password | No password in code or environment variables |
| Send reads to the reader endpoint | Spreads load across replicas |
RDS Proxy also shortens failover: applications keep their connection to the proxy while it switches to the new writer.
Where should an application on Lambda get its database password, so that the password can be rotated without a code change?
Show answer
From Secrets Manager at runtime, ideally cached. Rotation updates the secret, and the function picks up the new value without redeployment. IAM database authentication avoids the password altogether.
Exam Tips
Remember for the exam
- Too many connections from Lambda functions calls for RDS Proxy.
- To run SQL from Lambda without managing connections, use the RDS Data API with Aurora.
- To avoid database passwords, use IAM database authentication, or Secrets Manager with rotation.
- Send writes to the cluster (writer) endpoint and reads to the reader endpoint.