Question

Difficulty: MediumServerless Development with AWS Lambda

A developer has implemented an AWS Lambda function that is triggered by an Amazon SQS queue. The function retrieves database credentials from AWS Secrets Manager, connects to an Amazon RDS PostgreSQL database in a private subnet, and processes messages. During load testing, the database experiences connection exhaustion, and some SQS messages are processed multiple times. Which TWO actions should the developer take to resolve these issues?

  1. Move the database connection initialization and secrets retrieval code outside of the Lambda handler function.Answer
  2. Configure the SQS queue's visibility timeout to be at least six times the timeout configuration of the Lambda function.Answer
  3. C
    Hardcode the database credentials directly in the SDK client initialization within the Lambda function code to bypass retrieving secrets.
  4. D
    Decrease the SQS queue's visibility timeout to a value less than the Lambda function's timeout to speed up processing.
  5. E
    Associate the Lambda function with the private subnets containing the database without creating a NAT Gateway or VPC endpoint.

Answer

Initialize the database connection and secrets retrieval outside of the handler function, and configure the SQS queue's visibility timeout to be at least six times the Lambda function's timeout.
Moving the connection code outside the handler allows subsequent invocations to reuse the established database connections from the execution context. Configuring the SQS visibility timeout to be at least six times the Lambda function's timeout prevents messages from being visible to other pollers before the active execution has completed.

Step-by-Step Solution

1
Analyze the database connection exhaustion issue.
The Lambda function is creating a new connection on every invocation. By moving the connection logic outside the handler, the function leverages execution context reuse.
This maintains persistent database connection pools across multiple invocations in the same execution container.
2
Analyze the duplicate message processing issue.
If the execution time of the function exceeds the SQS queue's visibility timeout, messages become visible to other function instances while still processing.
Setting the visibility timeout to at least six times the Lambda timeout provides a safety buffer for processing and retries.

Key Concept

Reusing execution contexts and managing integration timeouts are critical for optimizing Lambda performance and ensuring message delivery semantics.
Estimated Time:1m 30s
Rate this question