A developer is building a smart agriculture application. An Amazon SNS topic receives soil moisture alerts, which are subscribed to by an Amazon SQS queue. An AWS Lambda function is configured to process messages from this SQS queue. The Lambda function has a timeout of 30 seconds. The SQS queue has a Default Visibility Timeout of 15 seconds. During testing, the developer notices that some alerts are being processed multiple times, even though the Lambda function executes successfully in 20 seconds. Which action should the developer take to prevent these duplicate invocations?
- Increase the visibility timeout of the SQS queue to at least 180 seconds.Answer
- BDecrease the SQS queue's visibility timeout to 5 seconds.
- CIncrease the Lambda function's timeout to 120 seconds while keeping the SQS queue's visibility timeout at 15 seconds.
- DModify the Lambda function's SDK client configuration to use hardcoded AWS credentials for deleting the processed messages.
Answer
Increase the visibility timeout of the SQS queue to at least 180 seconds.
The correct action is to increase the SQS queue's visibility timeout to at least 180 seconds. AWS recommends setting the visibility timeout of an SQS queue to at least 6 times the timeout of the subscribing Lambda function (6 * 30 seconds = 180 seconds). This configuration ensures that the message is not visible to other consumers while the function is running or retrying.
Step-by-Step Solution
Key Concept
AWS Lambda integration with Amazon SQS requires setting the SQS queue's visibility timeout to at least 6 times the Lambda function's timeout to prevent duplicate message processing during execution and retries.