How Lambda Works with Amazon RDS Proxy - Overview
Scope:
- Intro,
- The concept: RDS Proxy with Lambda,
- Problem RDS Proxy solves,
- High-Level Architecture,
- How It Works — Step by Step,
- Benefits,
- Requirements,
- Best Practices,
- Sample Node.js Lambda with RDS Proxy Script,
- Architecture,
- Insights.
Intro:
- twtech Overview on how AWS Lambda works with Amazon RDS Proxy.
- This will cover architecture, execution flow, benefits, limitations, and best practices.
1.The concept: RDS Proxy with Lambda
Normally, AWS Lambda connects
directly to Amazon RDS (MySQL, PostgreSQL, or Aurora).
But Lambda has two key challenges:
- Database connection limits —
Each Lambda execution environment can open its own DB connection, and
rapid scaling can overwhelm RDS.
- Connection overhead —
Creating DB connections is slow (hundreds of milliseconds), which can hurt
performance.
Problem RDS Proxy solves:
- Pooling & reusing connections instead of opening new ones.
- Handling authentication securely via Secrets Manager.
- Improving resiliency
to database failovers.
2. High-Level Architecture (the conceptual layout):
3. How It Works —
Step by Step
- Lambda Initialization
- Lambda runs inside a VPC-enabled configuration (must
be in same VPC as RDS Proxy).
- Lambda’s IAM role has permissions to use the RDS Proxy
and fetch credentials from AWS Secrets Manager.
- First Connection
- Lambda connects to the RDS Proxy endpoint instead of
directly to the RDS instance.
- RDS Proxy already has pre-warmed, pooled
connections to the database.
- Authentication happens using credentials stored in AWS
Secrets Manager, which RDS Proxy retrieves automatically.
- Connection Reuse
- When Lambda scales out, each concurrent execution
connects to the proxy endpoint, not the DB.
- The proxy reuses idle DB connections for new requests
instead of opening fresh ones.
- Failover Handling
- If the RDS instance restarts or fails over to a
standby, RDS Proxy keeps connections alive and routes traffic to the new
primary without twtech Lambda function noticing.
- Connection Closing
- When Lambda finishes, it closes the proxy connection,
but the proxy does not drop the underlying DB connection — it
returns it to the pool.
4. Benefits
|
Benefit |
How It Helps Lambda |
|
Reduced DB load. |
Limits max concurrent DB
connections. |
|
Faster cold starts. |
Reuses pooled connections. |
|
Improved scalability. |
Handles thousands of Lambda
invocations without flooding DB. |
|
Secure
authentication. |
No hardcoded passwords — uses
Secrets Manager. |
|
Better failover
resilience. |
Smooth failover with minimal
downtime. |
6. Requirements
- Lambda must run inside the same VPC as
RDS Proxy.
- Subnets must have proper security group rules
allowing traffic to the proxy.
- The DB engine must be MySQL or PostgreSQL
(Aurora or RDS).
- Secrets Manager must store DB credentials.
7. Best Practices
- Set Max Connections in RDS Proxy — Usually ~2–4× DB max connections for pooling
efficiency.
- Use IAM Authentication for better security (if supported by DB engine).
- Reuse DB connections in Lambda code — Create the connection outside the handler so it
persists across invocations in the same execution environment.
- Monitor with CloudWatch Metrics — Look at DBConnections, ClientConnections, and DatabaseConnectionsCurrentlyBorrowed.
8. Sample Node.js Lambda with RDS Proxy Script
# javascript
const mysql = require('mysql2/promise');
let connection;
exports.handler
= async (event) => {
if (!connection) {
connection = await mysql.createConnection({
host: process.env.RDS_PROXY_ENDPOINT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});
}
const [rows] = await connection.execute('SELECT
NOW() AS current_time');
return rows[0];
};
NB:
- connection is outside the handler, so it’s reused between Lambda invocations in the same container.
twtech-Insights:
- VAN usually refers to Value-Added Network.
- This is a private network that provides secure and reliable data transmission services for businesses.
These networks often offer additional services like:
- Data validation,
- Formatting,
- Encryption,
- Communication protocol management.
VANs are frequently used to:
- Facilitate secure EDI (Electronic Data Interchange) transactions between businesses, which can then be integrated with cloud-based solutions.
No comments:
Post a Comment