Question

Difficulty: HardServerless Development with AWS Lambda

An organization has deployed a retail transaction processing system. The core component is a serverless function that handles purchase validation. This function runs inside private subnets of an Amazon VPC to connect to a secure backend database cluster. To finalize transactions, the function must also perform an outbound HTTPS call to an external payment processor.

During high-traffic periods, two issues are observed:
1. The function fails to connect to the external payment processor, resulting in network connection timeouts.
2. The backend database cluster runs out of available database connections, causing transaction failures.

Which combination of architectural and code modifications should the developer implement to resolve these issues? (Select TWO.)

  1. Deploy a NAT Gateway in a public subnet, and add a route in the private subnets' route table directing destination 0.0.0.0/00.0.0.0/0 to the NAT Gateway.Answer
  2. Declare and initialize the database connection client outside of the Lambda handler method, enabling reuse across warm execution contexts.Answer
  3. C
    Associate the Lambda function with public subnets in the VPC and enable the Assign Public IP configuration option in the function's network settings.
  4. D
    Attach an Internet Gateway directly to the private subnets where the function is deployed and configure a route for 0.0.0.0/00.0.0.0/0 to point to the Internet Gateway.
  5. E
    Create and destroy the database connection pool inside the Lambda handler function on each invocation to ensure connections do not remain open during idle times.

Answer

Configure a NAT Gateway in a public subnet to route outbound traffic from the private subnets, and declare the database connection client outside the Lambda handler method to reuse connections across execution context instances.
To resolve the network connectivity issues, the Lambda function needs outbound internet access. Since it runs in a private VPC subnet, a NAT Gateway must be deployed in a public subnet, and the private subnet's route table must route all outbound traffic (0.0.0.0/00.0.0.0/0) to the NAT Gateway. To resolve database connection exhaustion, the database connection pool must be declared outside the handler function. This ensures that the connection pool persists across warm starts of the Lambda execution context, reducing the total number of connections opened to the database.

Step-by-Step Solution

1
Analyze the network timeout issue.
Since the Lambda function is deployed within private subnets of a VPC, it lacks direct access to the public internet. It cannot reach the external payment processor's HTTPS endpoint without a network address translation device.
Identifying that resources in a private VPC subnet require a NAT Gateway or similar NAT device placed in a public subnet with an attached Internet Gateway to initiate outbound connections.
2
Formulate the network resolution.
Provision a NAT Gateway in a public subnet, attach an Internet Gateway to the VPC, and add a route in the private subnets' route table directing 0.0.0.0/00.0.0.0/0 to the NAT Gateway.
This establishes a secure, outbound path to the public internet for the Lambda function.
3
Analyze the database connection exhaustion issue.
If connection establishment is performed inside the handler function, every single invocation creates a new connection, which exhausts the database's connection limits under heavy concurrency.
Understanding Lambda execution context lifecycle and cold/warm starts.
4
Formulate the code optimization resolution.
Move the database connection pool initialization outside the handler function to the initialization phase of the container.
The execution context is reused for subsequent invocations, keeping the database connection alive and shared, preventing the overhead of re-establishing connections on every invocation.

Key Concept

VPC networking for outbound Lambda traffic and execution context reuse for connection management.
Estimated Time:2m 0s
Rate this question