A developer is building a serverless application where an Amazon S3 bucket triggers an AWS Lambda function asynchronously when new files are uploaded. The Lambda function processes each file and performs data extraction. Under heavy load, some files fail to process. The developer wants to ensure that any failed event payload is automatically sent to an Amazon SQS queue for manual analysis, rather than being discarded. Which of the following is the AWS-recommended method to achieve this?
- Configure an On-Failure Lambda Destination for the function and specify the SQS queue as the destination.Answer
- BImplement a try-catch block inside the Lambda function handler to capture errors and use the AWS SDK to write the payload to the SQS queue.
- CModify the trust policy of the Lambda execution role to allow the SQS service to write directly to the queue when the function execution fails.
- DIncrease the visibility timeout of the SQS queue to be at least six times the timeout of the Lambda function to ensure failed messages are retried.
Answer
Configure an On-Failure Lambda Destination for the function and specify the SQS queue as the destination.
The correct approach is to configure an On-Failure Lambda Destination. For asynchronous invocations, Lambda can route execution records to external services like SQS on failure. This handles all execution errors, including application-level errors, timeouts, and out-of-memory crashes.
Step-by-Step Solution
Key Concept
AWS Lambda Asynchronous Invocation Destinations