AWS Lambda with RDS Proxy
AWS Lambda can scale quickly, but each concurrent invocation may open a database connection. Amazon RDS Proxy pools and reuses connections, helping protect your database from sudden traffic spikes. AWS recommends it for Lambda workloads that make frequent, short database connections.
Here’s how to connect Lambda to an existing RDS PostgreSQL or MySQL database using IAM authentication between Lambda and the proxy, and Secrets Manager credentials between the proxy and database.
Configure the network
For a straightforward setup, place Lambda, RDS Proxy, and the database in the same VPC. Configure security groups to allow Lambda to reach the proxy, and the proxy to reach the database, on the database port—typically
5432 for PostgreSQL or 3306 for MySQL. Use security group references to restrict access:Security group | Direction | Allow |
|---|---|---|
Lambda | Outbound | Database port to proxy security group |
Proxy | Inbound | Database port from Lambda security group |
Proxy | Outbound | Database port to database security group |
Database | Inbound | Database port from proxy security group |
These rules permit network connectivity between the components.
Store database credentials
Create a database user with the permissions your application needs, then store its username and password in AWS Secrets Manager. The secret must match an existing database account. RDS Proxy uses these credentials to open database connections.
Create the proxy
Create an RDS Proxy, select your database as its target, and associate the secret. Assign the proxy an IAM role that can read the secret, choose subnets in at least two Availability Zones, require IAM authentication from clients, and enable TLS. Wait until the proxy and its database target are available.
Give the proxy permission to read the database secret and decrypt it:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "secretsmanager:GetSecretValue", "Resource": "SECRET_ARN" }, { "Effect": "Allow", "Action": "kms:Decrypt", "Resource": "KMS_KEY_ARN", "Condition": { "StringEquals": { "kms:ViaService": "secretsmanager.REGION.amazonaws.com" } } } ]}Use the full secret ARN, including its generated suffix. For a customer managed KMS key, ensure its key policy also permits this access. The proxy role’s trust policy must allow
rds.amazonaws.com to perform sts:AssumeRole . Amazon Relational Database Service Lambda does not need secret-reading permissions for this flow; the proxy retrieves the password.
Connect Lambda
Grant Lambda’s execution role
rds-db:connect for the proxy resource ID and database username. Add this policy so Lambda can connect as a specific database user through the proxy:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "rds-db:connect", "Resource": "arn:aws:rds-db:REGION:ACCOUNT_ID:dbuser:prx-PROXY_RESOURCE_ID/app_user" } ]}Replace the placeholders with your values. Use the proxy’s resource ID starting with
prx- , and make app_user match the database username. IAM authentication requires a TLS connection.
Also:
- Attach
AWSLambdaVPCAccessExecutionRolefor VPC network interface management and CloudWatch logging. AWS Lambda
Set the role’s trust policy to allow
lambda.amazonaws.com to perform sts:AssumeRole .In your function, generate an IAM authentication token using the proxy endpoint, port, username, and AWS Region. Use that token as the password when opening a TLS connection through your PostgreSQL or MySQL driver. Your connection hostname must be the proxy endpoint.
Test the connection
Invoke Lambda with a simple query such as
SELECT 1 . If it fails, check security groups, IAM permissions, the database username, and the credentials stored in Secrets Manager.RDS Proxy reduces connection overhead, but queries still consume database capacity. Keep transactions short and monitor database load as Lambda concurrency grows.
Important Considerations
Before moving to production, consider these points:
- Control concurrency. Connection pooling helps manage connections, but database CPU, memory, and query capacity remain limits. Use Lambda reserved concurrency to keep traffic within tested capacity.
- Watch for connection pinning. Session settings and certain SQL operations can tie a client to a dedicated database connection, reducing pooling efficiency. Keep transactions short and test your ORM’s behavior.
- Reuse connections carefully. Reuse healthy connections across warm Lambda invocations and reconnect when stale. Generate a fresh IAM token when opening a new connection; tokens expire after 15 minutes, but expiration does not terminate an established session.
- Plan for failures. Set connection and query timeouts within Lambda’s execution budget. Use bounded retries for transient failures and make database writes idempotent so retries cannot create duplicate results.
- Monitor under realistic load. Track database utilization,
DatabaseConnectionsBorrowLatency, andDatabaseConnectionsCurrentlySessionPinned. Test traffic spikes and database failover before release.
- Account for cost. RDS Proxy adds charges based on database capacity. Compare its connection-management benefits and added network latency against your workload’s needs.
Conclusions
RDS Proxy is a useful fit for Lambda applications with bursty traffic and frequent database connections. Its benefits depend on careful IAM configuration, efficient transactions, sensible concurrency limits, and production monitoring. Validate the design with your actual queries and traffic before relying on it at scale.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article