Question

Difficulty: MediumServerless Development with AWS Lambda

A developer is implementing an AWS Lambda function that processes transaction records and stores them in an Amazon RDS database. The developer wants to optimize the database connection management to avoid connection overhead on subsequent invocations, while ensuring that the function does not exhaust the database's max connection limit during scaling. Which configuration and coding pattern should the developer implement?

  1. Initialize the database connection pool outside the Lambda handler function to enable execution context reuse, and set the function's reserved concurrency to limit the maximum number of concurrent executions.Answer
  2. B
    Initialize the database connection pool inside the Lambda handler function, relying on AWS Lambda to automatically close and clean up idle database connections when the invocation completes.
  3. C
    Configure the Lambda function to run inside the public subnets of the VPC where the RDS database is located, and configure the RDS security group to allow inbound traffic from the Lambda function's elastic network interfaces (ENIs).
  4. D
    Add an IAM trust policy to the database credentials stored in AWS Secrets Manager that permits the Lambda function's IAM execution role to assume the RDS service role to manage the connection pool.

Answer

Initialize the database connection pool outside the Lambda handler function to enable execution context reuse, and set the function's reserved concurrency to limit the maximum number of concurrent executions.
The correct answer is to initialize the connection pool outside the handler and configure reserved concurrency. Declaring the database client/pool in the initialization code (outside the handler) allows the function to reuse existing TCP connections during subsequent invocations (warm starts), minimizing connection setup latency. Furthermore, relational databases like Amazon RDS have a maximum limit on concurrent connections. Since Lambda scales out horizontally by spawning new execution environments, configuring reserved concurrency on the Lambda function restricts the maximum number of concurrent executions, thereby capping the total number of active connections the Lambda function can open to the database.

Step-by-Step Solution

1
Analyze the requirements for database connection reuse and concurrency management in AWS Lambda.
Identified that reusing database connections requires utilizing execution context reuse (declaring the connection client/pool outside the handler method).
Variables declared outside the handler function remain initialized in the execution environment and can be reused in subsequent warm starts.
2
Determine how to protect the downstream relational database (Amazon RDS) from connection exhaustion during high-concurrency events.
Identified that setting reserved concurrency on the Lambda function establishes a hard limit on the number of concurrent execution environments.
Since each execution environment can only process one event at a time and maintains its own connection pool, limiting the concurrency limits the total number of connections to the database.

Key Concept

AWS Lambda execution context reuse and reserved concurrency.
Estimated Time:1m 30s
Rate this question