Lesson 6 of 7 Amazon S3

Processing New Objects with S3 Event Notifications

This lesson explains how S3 event notifications start processing as soon as objects are created, deleted or restored. They are the usual way to build file-processing pipelines such as thumbnail generation.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the destinations and event types that S3 event notifications support.
  • Explain why S3 needs permission in the function's resource-based policy to invoke it.
  • Describe the delivery behaviour that consumers must handle, such as at-least-once delivery and URL-encoded keys.
  • Choose between overlapping notification rules, Amazon SNS fan-out and Amazon EventBridge when several consumers need the same event.

Destinations and event types

S3 event notifications send a message when something happens to an object, such as an upload or a deletion, so applications react without polling the bucket. Notifications are delivered at least once and usually within seconds, but not necessarily in order. Each notification rule selects event types and, optionally, a key prefix and suffix, such as uploads/ and .jpg, and sends matching events to one of four destinations.

A Lambda function processes each object as soon as it arrives, for example to create a thumbnail. An SQS queue holds events until consumers are ready, which protects a slower processor from bursts of uploads. An SNS topic fans one event out to several subscribers, such as a queue for processing and an email for monitoring. Two rules with overlapping prefixes cannot send the same event type to different destinations, so SNS fan-out is the usual way to deliver one event to several consumers. When EventBridge is enabled on the bucket, S3 sends every event to the default event bus, where rules can filter on any field, such as object size or metadata, and route events to many target types, with archive and replay.

The following table summarises the four destinations.

DestinationSuits
LambdaProcessing each object immediately
SQSBuffering events for consumers that process at their own pace
SNSFanning one event out to several subscribers
EventBridgeRich filtering on any event field, archive and replay, and many target types

Common event types are s3:ObjectCreated:* (Put, Post, Copy and CompleteMultipartUpload), s3:ObjectRemoved:*, s3:ObjectRestore:Completed and s3:LifecycleExpiration:*.

Configuring a notification

Example. Invoke a function for JPEG uploads under uploads/. S3 must be allowed to invoke the function, so the function's resource-based policy is updated first.

aws lambda add-permission --function-name make-thumbnail --statement-id s3-invoke \
  --action lambda:InvokeFunction --principal s3.amazonaws.com \
  --source-arn arn:aws:s3:::photo-uploads --source-account 111122223333

aws s3api put-bucket-notification-configuration --bucket photo-uploads --notification-configuration '{
  "LambdaFunctionConfigurations": [{
    "LambdaFunctionArn": "arn:aws:lambda:eu-west-1:111122223333:function:make-thumbnail",
    "Events": ["s3:ObjectCreated:*"],
    "Filter": {"Key": {"FilterRules": [{"Name": "prefix", "Value": "uploads/"}, {"Name": "suffix", "Value": ".jpg"}]}}
  }]
}'

SQS and SNS destinations need a queue or topic policy allowing s3.amazonaws.com, scoped with aws:SourceArn; EventBridge delivery is switched on per bucket and needs no destination policy.

Delivery behaviour

S3 event notifications have particular delivery characteristics that consumers must take into account.

  • Events are delivered at least once, usually within seconds, so consumers must be idempotent.
  • Object keys in the event are URL-encoded; a key containing a space arrives as + and must be decoded.
  • Two notification rules on the same bucket cannot have overlapping prefixes and suffixes for the same event type; routing through EventBridge removes this limit.
  • A function that writes output back to the prefix that triggers it creates a recursive loop; write output elsewhere.
Worked example

One upload, three consumers

When a video is uploaded, a transcoder, a virus scanner and an indexing service must all process it. S3 rejects a second notification rule because its prefix overlaps the first.

DesignOutcome
Three notification rules with the same prefixNot allowed: overlapping rules for the same event type
One notification to an SNS topic, with three SQS queues subscribedWorks: fan-out with independent buffering
EventBridge notifications with three rulesWorks, and each rule can filter on size, key or other fields

The team enables EventBridge on the bucket and creates three rules, one of which only matches objects larger than 1 GB for the transcoder. Each consumer handles duplicate events idempotently.

Check yourself

Can S3 send the same event type for the same prefix to two different Lambda functions?

Show answer

No. Notification configurations for the same event type cannot overlap in prefix and suffix. To reach several consumers, send the event to an SNS topic for fan-out, or enable EventBridge and use one rule per consumer.

Exam Tips

Remember for the exam

  • To process files as soon as they are uploaded, send S3 event notifications to Lambda, SQS or EventBridge.
  • S3 cannot invoke a function until the function's resource-based policy allows s3.amazonaws.com.
  • For filtering beyond prefix and suffix, or for many targets, enable EventBridge notifications.
  • Decode URL-encoded object keys in event handlers.
  • Break an S3 trigger loop by writing output to a different bucket or prefix.