Question

Difficulty: HardServerless Development with AWS Lambda

An application uses an AWS Lambda function to process incoming messages. The function is configured to connect to an Amazon Aurora PostgreSQL database inside a private subnet of a VPC. The function also needs to call an external partner API on the public internet to validate user information. During load testing, the developer observes two issues: the Lambda function fails to connect to the external partner API, resulting in network connection timeouts, and the Aurora database frequently runs out of database connections during concurrent invocation spikes.

Which combination of actions should the developer take to resolve these issues? (Select two.)

  1. Deploy the Lambda function in the private subnet, configure a NAT Gateway in a public subnet, and route external traffic (0.0.0.0/00.0.0.0/0) from the private subnet to the NAT Gateway.Answer
  2. Initialize the database connection pool outside the Lambda handler function to reuse connection contexts across warm invocations.Answer
  3. C
    Deploy the Lambda function in a public subnet of the VPC and enable public IP address assignment in the Lambda function configuration.
  4. D
    Initialize the database connection pool inside the Lambda handler function and increase the function's execution timeout to ensure connections are cleaned up after each invocation.
  5. E
    Configure the Amazon SQS visibility timeout of the source queue to be at least six times the Lambda function's timeout to allow the database to automatically close idle connections.

Answer

Deploy the Lambda function in the private subnet, route outbound internet traffic through a NAT Gateway, and initialize the database connection pool outside the handler function to allow reuse.
Deploying the Lambda function in private subnets with a NAT Gateway in a public subnet provides the necessary path to the public internet using static public IPs from the NAT Gateway. Additionally, initializing the database connection pool outside the handler ensures that the connection pool is persisted and reused across sequential warm invocations, preventing connection exhaustion.

Step-by-Step Solution

1
Analyze the network failure to the external API.
The Lambda function is associated with a VPC subnet but has no path to the public internet because VPC Lambdas do not get public IPs directly.
Resolving this requires placing the Lambda in private subnets and routing public traffic through a NAT Gateway in a public subnet.
2
Analyze the database connection exhaustion.
Creating database connections within the handler creates new connection instances for every execution, which does not leverage execution context reuse and overwhelms the database.
By moving connection initialization outside the handler, we reuse existing connections for subsequent warm invocations, reducing the overall connection count.

Key Concept

AWS Lambda VPC networking configuration and execution context reuse for database connection pooling.
Rate this question