0
0
AwsHow-ToBeginner · 4 min read

How to Use S3 Event Notification in AWS: Simple Guide

Use S3 event notifications to automatically trigger actions when objects in a bucket change. Configure the bucket to send events to targets like Lambda, SQS, or SNS by specifying event types and filters.
📐

Syntax

An S3 event notification configuration includes the bucket, event types, optional filters, and a destination. The destination can be an AWS Lambda function, an SQS queue, or an SNS topic.

Key parts:

  • Bucket: The S3 bucket to watch.
  • Event Types: Actions like s3:ObjectCreated:Put or s3:ObjectRemoved:Delete.
  • Filter: Optional prefix or suffix to limit which objects trigger events.
  • Destination: Where to send the event (Lambda, SQS, SNS).
json
{
  "LambdaFunctionConfigurations": [
    {
      "Id": "ExampleConfig",
      "LambdaFunctionArn": "arn:aws:lambda:region:account-id:function:function-name",
      "Events": ["s3:ObjectCreated:Put"],
      "Filter": {
        "Key": {
          "FilterRules": [
            {"Name": "suffix", "Value": ".jpg"}
          ]
        }
      }
    }
  ]
}
💻

Example

This example shows how to configure an S3 bucket to send notifications to a Lambda function when a new .jpg file is uploaded.

python
import boto3

s3 = boto3.client('s3')
lambda_arn = 'arn:aws:lambda:us-east-1:123456789012:function:ProcessImage'
bucket_name = 'my-example-bucket'

notification_configuration = {
    'LambdaFunctionConfigurations': [
        {
            'Id': 'ImageUploadNotification',
            'LambdaFunctionArn': lambda_arn,
            'Events': ['s3:ObjectCreated:Put'],
            'Filter': {
                'Key': {
                    'FilterRules': [
                        {'Name': 'suffix', 'Value': '.jpg'}
                    ]
                }
            }
        }
    ]
}

response = s3.put_bucket_notification_configuration(
    Bucket=bucket_name,
    NotificationConfiguration=notification_configuration
)

print('Notification configuration set:', response)
Output
Notification configuration set: {}
⚠️

Common Pitfalls

  • Not granting the correct permissions to the destination (e.g., Lambda needs permission to be invoked by S3).
  • Forgetting to add the event source permission to Lambda for S3 triggers.
  • Using incorrect event names or missing required event types.
  • Not setting filters correctly, causing unwanted or no events.
  • Trying to use event notifications on buckets with versioning enabled without considering version IDs.
python
## Wrong: Missing Lambda permission
# This will cause errors because Lambda cannot be triggered by S3

## Right: Add permission to Lambda
import boto3

lambda_client = boto3.client('lambda')
lambda_client.add_permission(
    FunctionName='ProcessImage',
    StatementId='s3invoke',
    Action='lambda:InvokeFunction',
    Principal='s3.amazonaws.com',
    SourceArn='arn:aws:s3:::my-example-bucket'
)
📊

Quick Reference

Remember these key points when using S3 event notifications:

  • Event types include object created, removed, restored, etc.
  • Destinations can be Lambda, SQS, or SNS.
  • Filters help target specific files by prefix or suffix.
  • Permissions must allow S3 to invoke the destination.
  • Use AWS SDK or Console to configure notifications.

Key Takeaways

Configure S3 event notifications by specifying event types, filters, and destinations like Lambda.
Always grant proper permissions for S3 to invoke your destination service.
Use filters to limit notifications to relevant objects by prefix or suffix.
Test your configuration by uploading files that match your event and filter criteria.
You can manage S3 event notifications via AWS Console, CLI, or SDKs like boto3.