Soru

Zorluk: OrtaServerless Development with AWS Lambda

A developer is configuring a serverless application where an Amazon Simple Queue Service (Amazon SQS) queue triggers an AWS Lambda function to process messages. During integration testing, the developer notices that some messages are being processed multiple times by different Lambda invocations, even though the Lambda function eventually runs successfully. The Lambda function is currently configured with an execution timeout of 15 seconds15\text{ seconds}. Which two actions should the developer take to resolve this issue? (Select two.)

  1. Increase the visibility timeout of the Amazon SQS queue to at least 90 seconds90\text{ seconds}.Cevap
  2. Ensure the Lambda function's execution timeout is configured to be greater than the maximum time required to process the batch of messages.Cevap
  3. C
    Set the SQS queue's visibility timeout to a value less than 15 seconds15\text{ seconds} to ensure that failed messages are quickly retried by other invocations.
  4. D
    Modify the Lambda function's IAM trust policy to grant the `sqs:ReceiveMessage` and `sqs:DeleteMessage` permissions to the SQS queue service principal.
  5. E
    Configure the Lambda function to run inside a private VPC subnet without a NAT Gateway or VPC endpoint to restrict SQS traffic to the internal AWS network.

Cevap

To resolve the duplicate processing issue, the developer must increase the SQS visibility timeout to at least 6 times the Lambda function's timeout (90 seconds90\text{ seconds}) and ensure the Lambda function's execution timeout is configured to be greater than the maximum time required to process the batch of messages.
To prevent duplicate processing of SQS messages by Lambda, the SQS queue's visibility timeout must be set to at least 6 times the Lambda function's timeout. With a 15 seconds15\text{ seconds} Lambda timeout, the visibility timeout must be at least 90 seconds90\text{ seconds} (15 seconds×615 \text{ seconds} \times 6). Additionally, the Lambda execution timeout itself must be configured to be greater than the maximum time needed to process a batch of messages to prevent premature termination.

Adım Adım Çözüm

1
Analyze the relationship between the SQS visibility timeout and the Lambda execution timeout.
Identify that if the SQS visibility timeout is shorter than the Lambda execution time (or not scaled correctly), SQS will make the message visible again before the Lambda function completes, leading to duplicate invocations.
This determines why messages are being reprocessed while the original invocation is still running.
2
Configure the SQS visibility timeout based on the Lambda timeout.
Increase the SQS visibility timeout to at least 6 times the Lambda timeout (15 seconds×6=90 seconds15\text{ seconds} \times 6 = 90\text{ seconds}).
This satisfies the AWS recommended best practice and prevents concurrent duplicate processing of messages that are still active.
3
Review and adjust the Lambda function timeout.
Ensure the Lambda function timeout is greater than the maximum batch processing time.
This prevents premature function termination, which would otherwise result in a failed execution and subsequent message reprocessing.

Anahtar Kavram

Integrating Amazon SQS with AWS Lambda event source mapping and managing visibility timeout and execution timeout relationships.
Bu soruyu puanla