How to Filter S3 Events by Object Size?
Posted on March 8, 2025 • 5 min read • 928 wordsStorage management or cost optimization, we explain how to filter S3 events by object size.

Filtering S3 events by object size is useful in various scenarios, especially when triggered actions depend on file volume. Here are some concrete use cases where this filtering is relevant:
AWS S3 sends events to EventBridge or an AWS Lambda function. Unfortunately, EventBridge rules do not directly allow filtering by object size. However, you can configure an AWS Lambda function to process events and apply filtering based on size.
s3:ObjectCreated:*) and forwards the data to an AWS Lambda function.event['Records'][0]['s3']['object']['size'] within the Lambda function.import json
def lambda_handler(event, context):
for record in event['Records']:
bucket_name = record['s3']['bucket']['name']
object_key = record['s3']['object']['key']
object_size = record['s3']['object']['size'] # Size in bytes
if object_size > 10_000_000: # Example: 10 MB
print(f"File {object_key} in {bucket_name} exceeds 10MB. Size: {object_size} bytes")
# Perform a specific action (e.g., store info, send an alert, etc.)
return {
'statusCode': 200,
'body': json.dumps('Filtering completed')
}You can then configure a destination to store these filtered events.
If you do not need real-time filtering but instead prefer periodic analysis:
SELECT key, size
FROM s3_inventory_table
WHERE size > 10000000; -- Filters objects larger than 10MBIf the goal is to automatically delete or move oversized files, you can:
Imagine you want to move all files larger than 100MB to a Glacier bucket.
In the AWS S3 → Batch Operations console:
| Criteria | AWS Lambda + EventBridge | S3 Inventory + Athena | S3 Batch Operations |
|---|---|---|---|
| Reactivity | Real-time | Delayed (periodic, based on inventory frequency) | Semi-automatic (requires manual task creation) |
| Complexity | Medium (requires a Lambda script) | Low (simple SQL query) | Medium (requires an Inventory file or object list) |
| Cost | Can be high (if triggered frequently) | Low (storage + Athena query costs) | Medium (charged per executed action) |
| Scalability | High (continuous event handling) | Very high (can analyze millions of objects) | High (processes millions of objects) |
| Primary Use Case | Triggering immediate actions on specific objects | Analyzing and reporting on a large number of objects | Mass actions (copying, deleting, modifying metadata) |
| Advanced Filtering | Yes (via Python code) | Yes (via advanced SQL queries) | Limited (based on the provided list) |
| Ease of Setup | Relatively simple (requires EventBridge + Lambda setup) | Easy (requires enabling Inventory + writing SQL) | Medium (simple configuration but requires an Inventory file) |
Filtering S3 events by object size is essential for cost optimization, automating large file processing, enhancing security, and improving monitoring. Depending on the need, different solutions exist: AWS Lambda with EventBridge, S3 Inventory with Athena, or S3 Batch Operations. Each approach offers a level of flexibility and performance suited to different use cases.
If your goal is real-time reaction, using AWS Lambda with EventBridge is recommended. If you prefer a more analytical and periodic approach, Athena with S3 Inventory is a solid solution. Finally, for automated actions on existing objects, S3 Batch Operations enables bulk file processing.
By applying these methods, you can better manage your AWS resources and optimize your S3 infrastructure.