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?
- 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.Cevap
- BInitialize 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.
- CConfigure 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).
- DAdd 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.
Cevap
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.
Adım Adım Çözüm
Anahtar Kavram
AWS Lambda execution context reuse and reserved concurrency.
Tahmini Süre:1m 30s