Question

Difficulty: MediumDebugging Lambda Execution and Configuration Issues

A developer is troubleshooting a PDF generation Lambda function. The function is configured to run inside private subnets of a VPC. It must retrieve document templates from an external public HTTPS endpoint and then save transaction logs to an Amazon RDS PostgreSQL database instance located in another private subnet of the same VPC. During testing, the developer observes two symptoms: the function consistently times out when attempting to reach the external HTTPS endpoint, and the RDS database runs out of available connection slots during concurrent test runs. Which two actions should the developer take to resolve these issues?

  1. Move the database connection client initialization code outside of the Lambda handler function.Answer
  2. Configure a NAT Gateway in a public subnet of the VPC and route internet-bound traffic from the private subnets through this gateway.Answer
  3. C
    Associate the Lambda function with the public subnets of the VPC and configure it to request public IP addresses.
  4. D
    Create a route in the private subnet's route table pointing directly to the VPC's Internet Gateway for destination 0.0.0.0/0.
  5. E
    Increase the Lambda function's timeout and memory configuration to force automatic garbage collection of the database connection pools on each invocation.

Answer

Initialize the database connection client outside of the Lambda handler function, and configure a NAT Gateway in a public subnet to route internet-bound traffic from the private subnets.
To resolve the RDS connection exhaustion, the database connection client must be initialized outside of the handler function. This enables the Lambda service to leverage execution context reuse, retaining the database connection pool across warm invocations rather than recreating it on every request. To resolve the internet connectivity issue, the Lambda function residing in the private subnet needs outbound internet access. Since Lambda functions do not receive public IP addresses, they cannot use an Internet Gateway directly; instead, traffic destined for the internet must be routed through a NAT Gateway situated in a public subnet.

Step-by-Step Solution

1
Diagnose the database connection exhaustion issue.
The database connection client is likely being initialized inside the Lambda handler function, causing a new database connection to open on every single invocation under load.
Identifying that new connections are opened per request guides the developer to optimize code structure using execution context reuse.
2
Diagnose the outbound internet connectivity timeout.
The Lambda function is placed in a private subnet and has no pathway to the public internet because it cannot communicate directly with an Internet Gateway without a public IP.
Understanding VPC routing rules explains why the connection to the external HTTPS endpoint is timing out.
3
Apply the solutions to both network and execution context issues.
Initialize the database client globally (outside the handler) to reuse connections, and route private subnet traffic through a NAT Gateway in a public subnet to enable internet access.
These steps address both the resource depletion and the networking blockages identified.

Key Concept

Debugging Lambda execution context reuse and VPC networking configurations.
Rate this question