Best Practices & Lambda Tips

Security, performance, and the connection gotcha when using RDS with Lambda.

1 min read

Security

  • Put databases in private subnets, and allow access only from the app's security group.
  • Store credentials in Secrets Manager (with rotation) or use IAM database authentication.
  • Encryption at rest is enabled at creation with KMS. Turn it on, because you can't simply flip it later.

Lambda + RDS: watch your connections

Each Lambda environment opens its own database connection. A traffic spike can create hundreds of environments and exhaust the database's connection limit.

Fixes:

  • RDS Proxy pools and shares connections between many Lambda invocations.
  • Open connections outside the handler so warm invocations reuse them.
  • Consider Aurora Serverless v2 with the RDS Data API for simple HTTP-based queries.

Performance and cost

  • Performance Insights shows which queries use the most database time.
  • Index columns you filter and join on, and check slow queries with EXPLAIN.
  • Right-size instances, and stop dev databases when not in use (RDS automatically restarts them after 7 days).
  • Use Graviton (db.*g) instance classes for better price-performance.
How it flows
  1. 1Many Lambdas

    Hundreds of invocations

  2. 2RDS Proxy

    Pools and reuses connections

  3. 3Database

    Sees a few connections

Key takeaways

  • Private subnets, Secrets Manager or IAM auth, and encryption at rest by default.
  • Use RDS Proxy to stop Lambda spikes from exhausting connections.
  • Use Performance Insights and indexes for speed, and right-size and stop idle dev DBs for cost.

Check your understanding

Pick an answer to see the explanation.

  1. 1. A Lambda traffic spike causes 'too many connections' errors on PostgreSQL. Best fix?

Next in “Run a classic web app”
You finished RDS & Aurora. Continue with the ECS & Fargate tutorial