Soru

Zorluk: Çok zorServerless Development with AWS Lambda

A developer is designing a serverless ordering system where an AWS Lambda function processes message batches from an Amazon SQS queue. The function is configured with a timeout of 2 minutes and is placed inside private subnets of a custom VPC to write data to an Amazon Aurora DB cluster. The function also needs to call a public endpoint of an external payment gateway. During initial testing under load, the developer observes that the Lambda function fails to reach the external payment gateway, resulting in connection timeouts. Furthermore, the Aurora database cluster runs out of available database connections very quickly, and SQS messages are being received and processed multiple times by different Lambda invocations, even though the executions complete successfully in 90 seconds. Which combination of steps should the developer take to resolve these issues?

  1. Initialize the database connection pool outside the Lambda handler function. Deploy a NAT Gateway in a public subnet, and configure the route table of the Lambda private subnets to route outbound traffic through the NAT Gateway. Increase the SQS visibility timeout to at least 12 minutes.Cevap
  2. B
    Initialize the database connection pool inside the Lambda handler function. Deploy the Lambda function in a public subnet, and configure the VPC route table to send outbound traffic through an Internet Gateway. Decrease the SQS visibility timeout to 15 seconds.
  3. C
    Initialize the database connection pool outside the Lambda handler function. Deploy a NAT Gateway in a public subnet, and configure the route table of the Lambda private subnets to route outbound traffic through the NAT Gateway. Decrease the SQS visibility timeout to 15 seconds to minimize message processing latency.
  4. D
    Initialize the database connection pool inside the Lambda handler function. Configure a VPC Endpoint for the database, and attach an Internet Gateway directly to the Lambda function's private subnets. Increase the SQS visibility timeout to at least 12 minutes.

Cevap

Initialize the database connection pool outside the Lambda handler function, route outbound private subnet traffic through a NAT Gateway in a public subnet, and increase the SQS visibility timeout to at least 12 minutes.
The correct solution addresses all three problems by: 1) Utilizing global scope variables to reuse the database connection pool across invocations, which mitigates connection exhaustion. 2) Routing Lambda outbound traffic through a NAT Gateway in a public subnet to allow communication with the external payment gateway. 3) Aligning the SQS visibility timeout to at least 6 times the Lambda function's timeout (12 minutes12\text{ minutes} for a 2-minute2\text{-minute} timeout) to prevent duplicate delivery of active messages.

Adım Adım Çözüm

1
Solve the internet connectivity issue for the VPC Lambda function.
Place the Lambda function in private subnets, deploy a NAT Gateway in a public subnet, and add a route in the private subnet's route table pointing 0.0.0.0/00.0.0.0/0 traffic to the NAT Gateway.
Lambda functions in a VPC do not receive public IP addresses. To access the public internet, they must route traffic through a NAT Gateway located in a public subnet that has a route to an Internet Gateway.
2
Resolve the Aurora database connection exhaustion issue.
Move the database connection pool initialization code out of the handler method and into the global initialization scope.
This allows the Lambda execution context to reuse the connection pool across warm invocations rather than establishing a new connection on every single invocation.
3
Resolve the duplicate SQS message processing issue.
Increase the SQS visibility timeout to at least 12 minutes, which is 6 times the Lambda function's timeout.
AWS recommends setting the SQS visibility timeout to at least 6 times the Lambda function timeout (6×2 minutes=12 minutes6 \times 2\text{ minutes} = 12\text{ minutes}). This ensures that the message remains invisible to other consumers until the Lambda function finishes processing or times out.

Anahtar Kavram

AWS Lambda VPC networking, execution context reuse, and SQS integration timeout alignment.
Tahmini Süre:3m 0s
Bu soruyu puanla