Question

Difficulty: MediumServerless Development with AWS Lambda

A developer is building a serverless application where an AWS Lambda function processes messages from an Amazon SQS queue. The function is configured to connect to an Amazon RDS MySQL database located in a private VPC subnet, and it also needs to make HTTPS requests to an external payment gateway API. The Lambda function's timeout is set to 15 seconds, and the SQS queue's visibility timeout is set to 10 seconds. During testing, the developer observes that messages are frequently being processed multiple times by the function, and all outbound requests to the payment gateway API fail due to network timeouts. Which configuration changes should the developer implement to resolve these issues?

  1. A
    Increase the Amazon SQS queue visibility timeout to at least 90 seconds, and deploy the Lambda function in a public subnet with a route to an Internet Gateway.
  2. B
    Decrease the Lambda function timeout to 5 seconds to match the SQS visibility timeout, and deploy the Lambda function in private subnets with a route to a NAT Gateway.
  3. Increase the Amazon SQS queue visibility timeout to at least 90 seconds, and deploy the Lambda function in private subnets with a route to a NAT Gateway in a public subnet.Answer
  4. D
    Increase the Amazon SQS queue visibility timeout to at least 90 seconds, and add a policy to the Lambda function's IAM trust policy to allow outbound traffic to the payment gateway.

Answer

Increase the Amazon SQS queue visibility timeout to at least 90 seconds, and deploy the Lambda function in private subnets with a route to a NAT Gateway in a public subnet.
The correct option successfully addresses the two core configuration issues. To prevent duplicate message processing, the SQS visibility timeout must be set to at least 6 times the function timeout (15 seconds×6=9015 \text{ seconds} \times 6 = 90 seconds). To grant the Lambda function internet access while keeping database connectivity, it must be deployed in private subnets with a route pointing to a NAT Gateway in a public subnet.

Step-by-Step Solution

1
Address duplicate message processing by comparing the SQS visibility timeout with the Lambda function timeout.
Identify that the current SQS visibility timeout (10 seconds) is shorter than the Lambda execution timeout (15 seconds), meaning messages become visible again while still processing.
According to AWS best practices, the SQS visibility timeout should be configured to at least 6 times the Lambda function timeout (15 seconds×6=9015 \text{ seconds} \times 6 = 90 seconds) to prevent duplicate processing from failures or retries.
2
Resolve the internet connectivity timeout issue for the VPC-attached Lambda function.
Route the outbound internet traffic from the private subnets hosting the Lambda function through a NAT Gateway located in a public subnet.
Lambda functions configured to run inside a VPC do not receive public IP addresses. Therefore, placing them in a public subnet with an Internet Gateway does not grant them internet access; they must use a NAT Gateway or VPC endpoint.

Key Concept

Configuring SQS visibility timeout relative to Lambda timeout and routing outbound Lambda VPC network traffic.
Rate this question