
3 Reasons Your VPC Lambda Is Slow (and How to Fix Each One)
VPC Lambda with Secrets Manager and RDS is slow on every invocation, even warm starts. 8 fixes in priority order, quickest wins first.
By Ramesh Chidirala and Carlos Calderon
If you're running AWS Lambda inside a VPC with Amazon RDS and AWS Secrets Manager, there's a good chance your function is spending hundreds of milliseconds to multiple seconds on overhead that has nothing to do with your business logic. On every single invocation.
This pattern is common in multi-tenant serverless applications. Three issues compound on every request, warm or cold: uncached secret fetches, cold start penalties, and new database connections. Most teams don't realize all three are happening simultaneously.
This post breaks down the three root causes, explains why they happen specifically in VPC Lambda architectures, and gives you 8 fixes in priority order (quickest wins first). All fixes are standard AWS services with no custom infrastructure required.
The architecture where this hits hardest
This pattern is common in multi-tenant serverless applications:
- AWS Lambda runs inside a VPC (for RDS access)
- Each request fetches credentials from AWS Secrets Manager
- Lambda connects to Amazon RDS (MySQL or PostgreSQL) per tenant
- Lambda calls Amazon Bedrock or another ML service for inference
- A scheduled "warmer" keeps execution environments hot
If this looks like your architecture, keep reading.
Issue 1: Secrets Manager latency from Lambda (hundreds of ms to multiple seconds)
What's happening
Every Lambda invocation calls the Secrets Manager API to fetch database credentials. Each call traverses the VPC network path, and if you don't have a VPC Interface Endpoint for Secrets Manager, that traffic routes through your NAT Gateway and the public internet before reaching the service endpoint.
Why it's so slow
- No response caching: Boto3's
get_secret_value()makes a fresh API call each time - there is no built-in response caching at the SDK level
- Full API round-trip: without a VPC endpoint, every secret fetch requires DNS resolution + TCP handshake + TLS negotiation + API processing + response - all routed through the NAT Gateway and public internet
- TLS handshake overhead: each new API call negotiates a TLS session (adds 30-50ms per call)
- SDK retry/backoff: if the first attempt is slow, the SDK's retry behavior can mask latency as 3+ second spikes
- Cold start compounding: first invocation pays cold start + first secret fetch simultaneously
Fix 1 (Highest Impact): AWS Parameters and Secrets Lambda Extension
The AWS Parameters and Secrets Lambda Extension runs a local HTTP cache inside your Lambda execution environment. Instead of calling the Secrets Manager API, you hit
localhost:2773 .import urllib.requestimport jsonimport os
def get_secret(secret_id): """Fetch from local extension cache. Sub-ms latency on cache hit vs full API round-trip.""" headers = {"X-Aws-Parameters-Secrets-Token": os.environ.get("AWS_SESSION_TOKEN")} url = f"http://localhost:2773/secretsmanager/get?secretId={secret_id}" request = urllib.request.Request(url, headers=headers) response = urllib.request.urlopen(request) return json.loads(response.read())["SecretString"]> Note: The official AWS docs now use the
requests library in their examples. The urllib.request approach above avoids adding a third-party dependency to your deployment package.Add the extension as a Lambda layer. The layer ARN varies by region - each region uses a different AWS account ID. Find the correct ARN for your region in the official ARN table :
# Example for us-east-1 only - check the docs for your region's ARNLayers: - arn:aws:lambda:us-east-1:177933569100:layer:AWS-Parameters-and-Secrets-Lambda-Extension:88This reduces Secrets Manager calls from N (per invocation) to approximately 1 per 5 minutes per execution environment. The default TTL is 300 seconds (5 minutes), configurable via the
SECRETS_MANAGER_TTL environment variable (range: 0-300 seconds).Key environment variables for tuning:
-
SECRETS_MANAGER_TTL- cache lifetime in seconds (default: 300)
-
PARAMETERS_SECRETS_EXTENSION_CACHE_SIZE- max cached items (default: 1000)
-
SECRETS_MANAGER_TIMEOUT_MILLIS- timeout for Secrets Manager requests (default: 0/no timeout)
Effort: 15 minutes. Change one function call + add a layer.
Alternative - Powertools for AWS Lambda: If you prefer a code-integrated approach, the Powertools parameters utility provides built-in caching with configurable TTL, JSON deserialization, and multi-provider support (Secrets Manager, Parameter Store, AppConfig). Ideal for Python, TypeScript, Java, or .NET runtimes.
Fix 2: VPC Interface Endpoint for Secrets Manager
Create a VPC Interface Endpoint for
com.amazonaws.\<region\>.secretsmanager in the subnets where your Lambda runs. This keeps traffic on the AWS backbone network instead of routing through the NAT Gateway and public internet.aws ec2 create-vpc-endpoint \ --vpc-id vpc-xxx \ --service-name com.amazonaws.us-east-1.secretsmanager \ --vpc-endpoint-type Interface \ --subnet-ids subnet-aaa subnet-bbb \ --security-group-ids sg-xxxEven with the Lambda Extension caching secrets, the initial fetch (on cold start or TTL expiry) still calls the API. The VPC endpoint ensures that call uses a low-latency private path.
Effort: 10 minutes. One CLI command or CloudFormation resource.
Fix 3: SDK Client Reuse Outside Handler
Initialize your Boto3 client outside the handler function. This reuses the TCP connection and TLS session across warm invocations (connection pooling at the SDK level):
import boto3
# Initialized ONCE per execution environment. Reused across invocations.secrets_client = boto3.client('secretsmanager')
def handler(event, context): # If you still need direct API calls (backup path), this reuses the TCP connection secret = secrets_client.get_secret_value(SecretId="my-secret") # ...This alone doesn't eliminate the API call, but ensures that when calls do happen, they reuse existing TCP connections and TLS sessions rather than establishing new ones.
Issue 2: Cold starts + unexplained execution lag
What's happening
Your single warmer invocation (a scheduled ping every 15 minutes) keeps only one execution environment warm. When concurrent requests arrive, Lambda scales horizontally and spins up new environments cold. Each cold environment pays the full initialization cost.
If you're seeing unexplained lag on warm invocations, it's likely three things happening serially during each request: VPC DNS resolution + Secrets Manager fetch (Issue 1) + RDS connection establishment (Issue 3).
Why the warmer doesn't work
Lambda scales horizontally. One warmer ping = one warm environment. If you get 5 concurrent requests, 4 of them hit cold environments regardless of your warmer.
Additionally:
- Large deployment packages (>50MB unzipped) increase cold start time
- Unused imports and heavyweight SDKs add initialization overhead
- VPC networking adds a small overhead for tunnel setup (though the multi-second ENI provisioning penalty was eliminated in 2019 with AWS Hyperplane ENIs - ENIs are now created at function deployment time, not at cold start)
Fix 4 (Highest Impact for Cold Starts): Provisioned Concurrency
Provisioned Concurrency pre-initializes execution environments, eliminating cold starts entirely for provisioned instances.
# Set Provisioned Concurrency (must use a published version or alias, not $LATEST)aws lambda put-provisioned-concurrency-config \ --function-name my-function \ --qualifier prod \ --provisioned-concurrent-executions 5
# Auto Scale (scale down at night, up during business hours)aws application-autoscaling register-scalable-target \ --service-namespace lambda \ --resource-id function:my-function:prod \ --scalable-dimension lambda:function:ProvisionedConcurrency \ --min-capacity 2 \ --max-capacity 10Use Application Auto Scaling to adjust based on schedule or utilization so you're not paying for idle capacity overnight. AWS recommends adding a 10% buffer above your typical peak concurrency.
Important: You cannot use Provisioned Concurrency with the
$LATEST version - you must publish a version or use an alias.Effort: 30 minutes. CLI commands + auto-scaling policy.
Fix 4b (Zero-Cost Alternative): Lambda SnapStart
If Provisioned Concurrency isn't viable (cost, unpredictable traffic), consider Lambda SnapStart . SnapStart takes a Firecracker microVM snapshot of your initialized execution environment at deploy time and resumes from that snapshot on cold start - reducing initialization time by up to 10x with no per-unit cost.
Supported runtimes: Python 3.12+, .NET 8+, Java 11+
# SAM template exampleMyFunction: Type: AWS::Serverless::Function Properties: Runtime: python3.12 SnapStart: ApplyOn: PublishedVersions AutoPublishAlias: liveKey constraints:
- SnapStart and Provisioned Concurrency are mutually exclusive - you cannot use both
- Does not support Amazon EFS or ephemeral storage > 512 MB
- Only works on published function versions (not
$LATEST)
- Your code must be resilient to snapshot/resume (avoid caching unique state during init)
When to choose SnapStart over Provisioned Concurrency:
- Your traffic is unpredictable (SnapStart has no idle cost)
- You want cold start reduction without managing capacity
- You're using Python 3.12+, Java 11+, or .NET 8+
When to choose Provisioned Concurrency:
- You need guaranteed sub-100ms cold starts (SnapStart is "sub-second", not instant)
- You use Node.js, Ruby, or other unsupported runtimes
- You need EFS or large ephemeral storage
💡 If neither Provisioned Concurrency nor SnapStart fits your use case:
Fix 5 (Alternative): Fan-Out Warmer Pattern
Instead of one warmer ping, send N concurrent invocations to keep N environments warm:
# warmer_lambda.py - invoked by EventBridge every 5 minutesimport boto3import time
lambda_client = boto3.client('lambda')
DESIRED_WARM_ENVS = 5
def handler(event, context): import concurrent.futures with concurrent.futures.ThreadPoolExecutor(max_workers=DESIRED_WARM_ENVS) as executor: futures = [ executor.submit( lambda_client.invoke, FunctionName="my-multi-tenant-function", InvocationType="RequestResponse", Payload=b'{"warm_up": true}' ) for _ in range(DESIRED_WARM_ENVS) ]> Important: For this pattern to actually warm N separate environments, the target function must run long enough for invocations to overlap in time. Add a short
time.sleep(0.1) in the warm-up code path of your target function, or the invocations may serialize and only keep 1-2 environments warm.This keeps multiple environments warm. For multi-tenant applications, include tenant-specific payloads to warm tenant-specific code paths (DB connections, config loading).
Fix 6: Optimize Deployment Package
Target: <10MB zipped.
- Use Lambda Layers for shared dependencies (
boto3,pymysql,psycopg2)
- Strip unused SDK sub-modules
- Use
pip install --targetwith selective inclusion
- Remove test files, docs, and
__pycache__from packages
Every MB in your deployment package adds milliseconds to cold start initialization.
Issue 3: Lambda-to-RDS connection time (slower than EC2 in the same VPC)
What's happening
Lambda creates a new TCP connection to RDS on every invocation. Unlike EC2 (which maintains persistent connection pools), Lambda execution environments are ephemeral. Each invocation opens a fresh MySQL/PostgreSQL connection.
That's a 3-way TCP handshake + database authentication handshake = typically 30-100ms per request in the same AZ (up to 200ms cross-AZ or under load), even when the Lambda environment itself is warm.
Why it's worse for multi-tenant
- Each tenant might use different credentials or schemas
- Connection objects initialized for Tenant A can't be reused for Tenant B
- Under concurrent load, Lambda can exhaust RDS
max_connections, causing connection queuing
- You can't simply cache one global connection
Fix 7 (Highest Impact): Amazon RDS Proxy
Amazon RDS Proxy maintains a warm pool of database connections. Lambda connects to the proxy (fast, already-established connection) instead of RDS directly (cold TCP handshake).
aws rds create-db-proxy \ --db-proxy-name my-app-proxy \ --engine-family MYSQL \ --auth '[{"AuthScheme":"SECRETS","SecretArn":"arn:aws:secretsmanager:us-east-1:ACCOUNT:secret:my-db-creds"}]' \ --role-arn arn:aws:iam::ACCOUNT:role/rds-proxy-role \ --vpc-subnet-ids subnet-aaa subnet-bbb \ --vpc-security-group-ids sg-xxxYour Lambda connects to the proxy endpoint instead of the direct RDS endpoint:
import pymysqlimport os
# Connection to RDS Proxy - pooled, fastconnection = pymysql.connect( host=os.environ["RDS_PROXY_ENDPOINT"], # proxy endpoint, not direct DB user="lambda_user", password=get_secret("my-db-creds"), database="mydb", connect_timeout=3, read_timeout=10, write_timeout=10, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)Additional benefits:
- Connection multiplexing: hundreds of concurrent Lambda invocations share a small pool of actual DB connections via transaction-level reuse
- No
max_connectionsexhaustion: the proxy manages the pool
- Native Secrets Manager integration: the proxy can fetch and rotate credentials itself
- IAM authentication support: use standard IAM authentication (proxy handles DB credentials via Secrets Manager) or end-to-end IAM authentication (eliminates Secrets Manager from the auth flow entirely - the proxy authenticates both client and database via IAM)
Fix 8: Connection Reuse Outside Handler
Initialize the connection outside the handler so warm invocations reuse it:
import pymysqlimport os
# ✅ Initialized ONCE per execution environmentconn = None
def get_connection(): global conn if conn is None or not conn.open: conn = pymysql.connect( host=os.environ["RDS_PROXY_ENDPOINT"], user="lambda_user", password=get_secret("my-db-creds"), database="mydb", connect_timeout=3 ) return conn
def handler(event, context): db = get_connection() # Reuses existing connection on warm invocations # ...This doesn't help the first invocation, but every subsequent warm invocation skips the connection handshake entirely.
The before and after

Before (problem architecture):
Lambda (VPC) -\> Secrets Manager API call (every invocation, no cache, through NAT Gateway) -\> Direct RDS connection (new TCP handshake every invocation) -\> Single warmer (keeps 1 environment warm) -\> Large deployment packageAfter (optimized architecture):
Lambda (VPC, Provisioned Concurrency or SnapStart) -\> Parameters & Secrets Extension (local cache, sub-ms on hit) -\> Secrets Manager VPC Interface Endpoint (fast path when cache expires) -\> RDS Proxy (warm connection pool, multiplexed) -\> SDK clients initialized outside handler (connection reuse) -\> Optimized deployment package (\<10MB)Priority order: quickest wins first
Priority | Fix | Addresses | Effort | Impact |
|---|---|---|---|---|
🔴 1 | Parameters & Secrets Lambda Extension | Secrets Manager latency | 15 min | Highest: eliminates per-invocation API call |
🔴 2 | RDS Proxy | DB connection per invocation | 1-2 hours | High: eliminates TCP handshake per request |
🟡 3 | SDK client + connection init outside handler | Repeated initialization | 15 min | Medium: reuses warm connections |
🟡 4 | VPC Interface Endpoint for Secrets Manager | NAT Gateway traversal | 10 min | Medium: fast path on cache miss |
🟢 5 | Provisioned Concurrency or SnapStart | Cold starts | 30 min | High for cold starts specifically |
🟢 6 | Deployment package optimization | Cold start duration | 1 hour | Low-medium: reduces init time |
Start at the top. Fix 1 alone should show the most dramatic improvement since it eliminates an external API call that's happening on every single invocation.
Further reading
- Using the AWS Parameter and Secrets Lambda Extension - AWS Compute Blog with performance benchmarks
- Use Secrets Manager secrets in Lambda functions - official tutorial with extension + Powertools approaches
- Improving startup performance with Lambda SnapStart - cold start optimization for Python, Java, .NET
- Using Amazon RDS Proxy with AWS Lambda - connection pooling and IAM auth
- RDS Proxy multiplexing for PostgreSQL Extended Query Protocol - deep dive on connection reuse
- Tutorial: Lambda function to access RDS via Proxy - step-by-step console walkthrough
- RDS Proxy connection tuning - MaxConnectionsPercent, IdleClientTimeout, ConnectionBorrowTimeout
- Lambda best practices - official guidance on initialization outside handler
- Managing Lambda Provisioned Concurrency - setup and auto-scaling
- Announcing improved VPC networking for Lambda - Hyperplane ENI architecture (2019)
- Powertools for AWS Lambda - Parameters - code-integrated caching for secrets and parameters
About the authors
Ramesh Chidirala is a Sr. Solutions Architect at AWS with over two decades of experience in enterprise architecture and digital transformation, helping customers align business strategy and technical execution.
Carlos Calderon
is a Solutions Architect at AWS, serving as a technical advisor to ISV and SaaS companies on cloud architecture and generative AI. With a background in leading AWS data center operations, he works with customers from architecture through deployment and mentors SAs and engineers across the builder community.
Tags: AWS Lambda, Secrets Manager, RDS Proxy, VPC, Serverless, Performance, Multi-Tenant, SnapStart, Powertools
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article