Handling s3:TestEvent in Amazon S3 Event Notifications
Amazon S3 sends an s3:TestEvent message when an event notification is configured. Consumers must handle its structure separately from ordinary event messages.
When an event notification is configured for an S3 bucket, Amazon S3 automatically sends an s3:TestEvent message. Its structure differs from an ordinary S3 event notification, so consumers must handle it separately.
https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-content-structure.html
When you configure an event notification on a bucket, Amazon S3 sends a test message with the
s3:TestEvent.
Building
Create a CloudFormation stack template:
AWSTemplateFormatVersion: "2010-09-09"Description: Example of CloudWatch events not queueing to SSE SQSResources: Bucket: Type: AWS::S3::Bucket Properties: BucketEncryption: ServerSideEncryptionConfiguration: - ServerSideEncryptionByDefault: SSEAlgorithm: AES256 NotificationConfiguration: QueueConfigurations: - Event: 's3:ObjectCreated:Put' Queue: !GetAtt Queue.Arn PublicAccessBlockConfiguration: BlockPublicAcls: true BlockPublicPolicy: true IgnorePublicAcls: true RestrictPublicBuckets: true
Queue: Type: AWS::SQS::Queue Properties: QueueName: s3-event-notification-test-queue ReceiveMessageWaitTimeSeconds: 20
QueuePolicy: Type: AWS::SQS::QueuePolicy Properties: PolicyDocument: Version: '2008-10-17' Statement: - Effect: Allow Principal: Service: s3.amazonaws.com Action: - SQS:SendMessage - SQS:ReceiveMessage Resource: !GetAtt Queue.Arn Condition: StringEquals: aws:SourceAccount: !Ref AWS::AccountId Queues: - !Ref QueueDeploy the stack:
aws cloudformation deploy \ --template-file template.yaml \ --stack-name s3-event-notification-testTesting
Poll the SQS queue to confirm that the notification configuration is working:
aws sqs receive-message \ --queue-url https://sqs.ap-northeast-1.amazonaws.com/{AccountId}/s3-event-notification-test-queueYou should observe the s3:TestEvent message in the output, even if no objects have been added to the bucket.
{ "Messages": [ { "MessageId": "...", "ReceiptHandle": "...", "MD5OfBody": "...", "Body": "{\"Service\":\"Amazon S3\",\"Event\":\"s3:TestEvent\",\"Time\":\"2020-12-29T18:53:47.874Z\",\"Bucket\":\"s3-event-notification-test-bucket-xxxxxxxx\",\"RequestId\":\"...\",\"HostId\":\"...\"}" } ]}Cleaning Up
Delete the stack afterward:
aws cloudformation delete-stack --stack-name s3-event-notification-testConclusion
After the S3 bucket and its SQS event notification are deployed, an s3:TestEvent message arrives before any object is uploaded. Although this template configures only s3:ObjectCreated:Put, the test message is sent automatically when the notification configuration is attached. Unlike an ordinary S3 event notification, it has top-level Service and Event fields and does not contain a Records array.
Lambda functions and applications that consume this queue should detect s3:TestEvent and either skip it or handle it explicitly. Code that assumes every message contains Records may otherwise fail as soon as the notification configuration is created.
Related posts
Uploading to S3 Through CloudFront Signed URLs
CloudFront signed URLs let you upload to S3 through a custom domain—useful when direct S3 pre-signed URLs are not an option.
Sign in with Slack Using Cognito User Pools and OIDC
Federating Cognito user pools with Slack over OIDC and wiring "Sign in with Slack" into a Next.js app with Amplify.
Deploying FastAPI on AWS Lambda with Lambda Web Adapter
Containerizing a FastAPI backend and deploying it to a single Lambda function with Lambda Web Adapter and AWS CDK.
API Gateway WebSocket: Implementing a Mock Integration
Building an API Gateway WebSocket API entirely with mock integrations, returning canned responses with no backend Lambda involved.
AWS EventBridge Scheduler: Starting and Stopping EC2 on a Schedule
Starting and stopping EC2 instances on a cron schedule with EventBridge Scheduler calling the EC2 API directly, no Lambda involved.
