Question

Difficulty: HardDebugging Lambda Execution and Configuration Issues

A developer is troubleshooting a Node.js AWS Lambda function that processes events from an Amazon DynamoDB stream. The function is configured to run inside two private subnets of a custom VPC to write caching updates to an Amazon ElastiCache for Redis cluster in the same subnets. The function also makes HTTPS calls to an external third-party service to validate customer addresses. The developer observes two symptoms in Amazon CloudWatch Logs: first, the function fails to connect to the external address validation API, resulting in connection timeout errors; second, even when address validation succeeds, the function execution duration frequently runs close to the maximum configured timeout of 3030 seconds because the database connections in the connection pool remain active, preventing the Node.js event loop from exiting. Which two actions should the developer take to resolve these issues? (Select two.)

  1. Deploy a NAT Gateway in a public subnet of the VPC, and add a route in the route tables of the Lambda function's private subnets that targets the NAT Gateway for destination 0.0.0.0/00.0.0.0/0.Answer
  2. Set the context.callbackWaitsForEmptyEventLoop property to false in the Lambda handler code.Answer
  3. C
    Enable the public IP address setting in the Lambda function's VPC configuration and move the function ENIs to a public subnet.
  4. D
    Configure an Internet Gateway in the VPC, and update the route tables of the Lambda function's private subnets to route 0.0.0.0/00.0.0.0/0 directly to the Internet Gateway.
  5. E
    Increase the Lambda function's timeout configuration to 1515 minutes and implement a background process to force close connections after 2525 seconds.

Answer

Deploy a NAT Gateway in a public subnet of the VPC and route outbound traffic from the private subnets to it. Additionally, set context.callbackWaitsForEmptyEventLoop to false in the handler code.
To resolve the API connection timeout, the Lambda function requires internet access. Since it is located in private subnets, it cannot route traffic directly to an Internet Gateway or utilize public IPs. Instead, a NAT Gateway must be set up in a public subnet, and the route tables for the private subnets must direct outbound traffic to it. To resolve the event loop timeout, the callbackWaitsForEmptyEventLoop property on the context object must be set to false. This tells the Lambda runtime to return the response immediately after the callback is invoked, frozen in state, without waiting for the connection pool to empty.

Step-by-Step Solution

1
Diagnose the external API connection timeout issue.
Determine that the Lambda function is running in private subnets without outbound route access to the public internet.
VPC-associated Lambda functions in private subnets require a NAT Gateway or VPC endpoint to connect to public endpoints.
2
Resolve the network connectivity problem.
Create a NAT Gateway in a public subnet and add a route mapping 0.0.0.0/00.0.0.0/0 to the NAT Gateway in the private subnets' route tables.
This establishes a valid route for outbound internet traffic from the private subnets.
3
Diagnose the function timeout issue caused by open connections.
Identify that the Node.js event loop is waiting for the active database connection pool to ElastiCache to be empty before terminating the invocation.
The default behavior of Node.js in Lambda keeps the execution active until the event loop is empty.
4
Resolve the event loop delay in the handler code.
Configure context.callbackWaitsForEmptyEventLoop to false at the beginning of the handler function.
This instructs Lambda to return the callback response immediately, bypassing the empty event loop check.

Key Concept

Debugging Lambda execution context behavior and VPC routing configurations
Rate this question