1. A developer is optimizing a high-traffic REST API. The API calls a Lambda function that queries DynamoDB. CloudWatch metrics show that DynamoDB read capacity is under-utilized, but Lambda Duration averages 900 ms. X-Ray traces show 850 ms is spent waiting for Lambda to initialize a new SDK client and establish a TLS connection to DynamoDB on each invocation. The function is receiving consistent traffic with no cold starts. What is the MOST likely cause and fix?
- A. The Lambda function is running out of memory, causing garbage collection pauses. Increase the memory allocation to 1,024 MB.
- B. The DynamoDB client is being instantiated inside the Lambda handler function on every invocation. Move the client initialization to the module/global scope outside the handler.✓ Correct
- C. The Lambda function's execution role lacks the dynamodb:GetItem permission, causing silent retries that add latency. Add the correct IAM permission.
- D. API Gateway response caching is disabled. Enable API Gateway caching to reduce the number of Lambda invocations.
Explanation
Correct: When an SDK client is instantiated inside the handler, it is recreated on every invocation, including DNS resolution and TLS handshake setup, adding hundreds of milliseconds of overhead. Moving the DynamoDB client initialization to the global scope (outside the handler) means it is created once per execution environment and reused across all warm invocations, eliminating the per-invocation initialization cost. Wrong — Option A: Memory pressure and GC pauses would typically manifest as spiky duration increases correlated with memory usage metrics. The trace explicitly shows the time is spent on SDK client initialization and TLS, not computation. Wrong — Option C: Insufficient IAM permissions would cause AccessDeniedException errors, not silent latency. CloudWatch would show Lambda errors, not merely high duration. Wrong — Option D: API Gateway caching would reduce the total number of Lambda invocations, but when Lambda is invoked, the 850 ms initialization overhead inside the function would remain unchanged. Caching doesn't fix per-invocation inefficiency.