AWS Lambda Failed-Event Destination and Amazon Athena Analytics

AWS Lambda → Amazon S3 → Amazon Athena

Capture AWS Lambda failed events to Amazon S3 and analyze them with Amazon Athena for error insights and patterns using asynchronous invocations.

This pattern demonstrates how to use Amazon S3 as a failed-event destination for AWS Lambda asynchronous invocations.
A Lambda function implements business logic with success and failure scenarios.
When the Lambda function fails during asynchronous invocation, AWS automatically captures the failed event to S3 for analysis.
Amazon Athena with AWS Glue enables SQL-based analytics on failed events to identify error patterns and gain insights.
The pattern uses AWS CLI with --invocation-type Event to demonstrate asynchronous invocation.
Important: Lambda destinations only work with asynchronous invocations (S3, SNS, EventBridge) and stream-based sources (Kinesis, DynamoDB Streams, SQS). They do not trigger for synchronous invocations.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lambda-s3-athena-cdk-ts

Deploy

Clone the repository: <code>git clone https://github.com/aws-samples/serverless-patterns</code>Change directory: <code>cd lambda-s3-athena-cdk-ts</code>Install dependencies: <code>npm install</code>Deploy the CDK stack: <code>cdk deploy</code>


Testing

Get Lambda function name: FUNCTION_NAME=$(aws cloudformation describe-stacks --stack-name LambdaS3AthenaCdkStack --query 'Stacks[0].Outputs[?OutputKey==`LambdaFunctionName`].OutputValue' --output text)
Test successful request: aws lambda invoke --function-name $FUNCTION_NAME --invocation-type Event --payload '{"action": "process", "value": 10}' response.json
Test failed request: aws lambda invoke --function-name $FUNCTION_NAME --invocation-type Event --payload '{"action": "process", "value": -5}' response.json
Verify failed events in S3: aws s3 ls s3://$(aws cloudformation describe-stacks --stack-name LambdaS3AthenaCdkStack --query 'Stacks[0].Outputs[?OutputKey==`FailedEventsBucketName`].OutputValue' --output text)/ --recursive
Query errors with Athena: SELECT responsepayload.errortype, COUNT(*) FROM failed_events GROUP BY responsepayload.errortype;

Cleanup

Delete the stack: cdk destroy

Created by:

Marco Jahn

Marco Jahn

Senior Solutions Architect - ISV, Amazon Web Services

Follow on LinkedIn