Skip to content
Visual explainer 2h 30m to learn 5 steps

Route one S3 event to many consumers on AWS

Trace a single object-created event through SNS fan-out, EventBridge rules and SQS buffers and pick the right one for each consumer.

By Invicly Tech, , in AWS Cloud Engineering. About 10 minutes to read.

Topic 19 of 38

In short

When a file lands in S3, several parts of the system may care: the processor, a thumbnail maker and an audit log. S3 can send that object-created event to EventBridge, which matches it against rules, or to SNS, which copies it to every subscriber. Putting an SQS queue in front of each consumer lets each one work at its own pace and retry on its own.

Explained simply

Often one thing that happens matters to several parts of a system. When a user uploads a file, the processor must handle it, a thumbnail service may want a preview, and an audit log should record it. The upload is one event, and you want to deliver it to many consumers without each one asking S3 all the time.

AWS gives you two main tools. Amazon SNS is a broadcaster: you publish one message to a topic, and every subscriber gets its own copy. Amazon EventBridge is a router: events arrive on a bus, and rules decide, by looking at the event's content, where each one goes.

S3 can send its events to either. For EventBridge you switch on one bucket setting; S3 then sends events such as Object Created to the default event bus. You write rules that match, for example, files under uploads/ ending in .jpg.

Consumers are not all equally fast or reliable. Putting an SQS queue in front of each one means a slow or broken consumer only fills its own queue. The others carry on, and the waiting messages are processed once it recovers.

Press Play on a diagram to walk through it one step at a time.

MapOne S3 event, many consumers
S3: Object Created in uploads/
  • EventBridge default bus
    • Rule: prefix uploads/ to processing queue
    • Rule: suffix .jpg to thumbnail queue
    • Rule: all objects to audit log group
  • Or SNS topic
    • SQS queue: processor
    • SQS queue: thumbnails
    • Email: operations, for alerts only
CompareSNS vs EventBridge vs SQS
SNS
  • Push to all subscribers
  • Filter policies on attributes or body
  • Very high throughput, low latency
  • No storage of messages
EventBridge
  • Route by event content
  • Many AWS and SaaS sources
  • Archive and replay
  • Up to five targets per rule
SQS
  • Stores messages up to 14 days
  • Consumers pull at their own pace
  • Retries and DLQ
  • One consumer group per queue

In depth

Three ways S3 can announce a new object

S3 event notifications can go directly to an SQS queue, an SNS topic or a Lambda function, configured per event type with optional prefix and suffix filters. This is simple, but rules for the same event type cannot overlap in prefix and suffix across destinations, which makes several consumers of the same files awkward. The second way is SNS: S3 publishes to one topic, and the topic fans out to many queues. The third way is EventBridge: you enable it on the bucket and S3 sends every event to the default bus, where any number of rules can match, each with up to five targets. EventBridge events also include more detail and support archive and replay.

  • Direct: simplest, but overlapping filters are not allowed
  • SNS: one topic, many subscribers
  • EventBridge: rules match on content, many rules per event
  • All three deliver at least once

Event patterns in EventBridge

An event pattern is JSON with the same shape as the event, listing values that must match. Matching is exact unless you use content filters such as prefix, suffix, anything-but, numeric ranges or exists. Fields not mentioned are ignored. An S3 Object Created event has source aws.s3, detail-type Object Created, and a detail object with bucket.name, object.key, object.size and more. A target can receive the whole event or a transformed version built by an input transformer. EventBridge retries failed deliveries with backoff, by default for up to 24 hours and 185 attempts, and a per-target DLQ, which is an SQS queue, can catch events that still fail. Test a pattern before deploying it with aws events test-event-pattern.

FlowHow EventBridge handles one event
  1. 1
    Event arrives

    S3 puts Object Created on the default bus

  2. 2
    Rules evaluated

    Every rule's pattern is checked

  3. 3
    Input shaped

    Whole event or an input transformer

  4. 4
    Delivered

    To each target of each matching rule

  5. 5
    Retried or dead-lettered

    Up to 24 hours, then the target's DLQ

SNS fan-out and filter policies

An SNS standard topic pushes each message to all subscriptions: SQS queues, Lambda functions, HTTPS endpoints, email and others. A subscription filter policy narrows what it receives. By default the policy matches message attributes; setting FilterPolicyScope to MessageBody makes it match fields in the JSON body, which suits S3 events. For SQS subscribers, turn on raw message delivery if you want the original body without the SNS envelope. SNS is a good fit when many subscribers need the same messages and filtering is simple. EventBridge suits routing by rich content, many event sources, schema discovery, archive and replay.

Permissions and buffering

Every hop needs permission. S3 needs a topic or queue policy allowing s3.amazonaws.com to publish, limited with aws:SourceArn to your bucket. EventBridge needs an SQS queue policy allowing events.amazonaws.com with aws:SourceArn set to the rule's ARN. SNS needs a queue policy for sns.amazonaws.com with the topic's ARN. If a queue uses SSE-KMS with a customer managed key, the sending service also needs permission on that key; the AWS managed SQS key will not work for these senders. Missing permissions are the most common reason events vanish, so check the rule's FailedInvocations metric. Each consumer queue then gets its own visibility timeout, redrive policy and DLQ, so their failures stay separate.

  • Restrict with aws:SourceArn on every policy
  • Watch FailedInvocations on EventBridge rules
  • KMS-encrypted queues need key permissions too
  • One queue and DLQ per consumer

Learn it in 5 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Send S3 events to EventBridge~30m
  2. 2
    Write an event pattern rule~30m
  3. 3
    Fan out with SNS~30m
  4. 4
    Buffer each consumer with SQS~30m
  5. 5
    Compare filtering and delivery~30m

Key points

  • EventBridge routes by content: a rule's event pattern can match bucket name, key prefix, suffix and more.
  • SNS fans out: every subscription gets a copy, optionally narrowed by a filter policy.
  • An SQS buffer per consumer isolates failures, absorbs bursts and gives each consumer its own DLQ.
  • S3 must be told to send events to EventBridge; it is a bucket setting, off by default.
  • Each target needs a resource policy that lets the sending service deliver to it.

Key terms

Tap a card to flip it. 0/6 known

Examples

json
{
  "source": ["aws.s3"],
  "detail-type": ["Object Created"],
  "detail": {
    "bucket": { "name": ["invicly-lab-uploads"] },
    "object": { "key": [{ "prefix": "uploads/" }] }
  }
}

An EventBridge rule pattern that matches new objects under uploads/ in one bucket.

Route new uploads to a processing queue with EventBridge

Prerequisites: the uploads bucket and an SQS queue files-work. Step 1: turn on EventBridge notifications for the bucket. Step 2: create a rule on the default bus with a pattern for Object Created under uploads/. Step 3: add the queue as a target and give it a queue policy that allows events.amazonaws.com from this rule's ARN. Step 4: upload a test file. Evidence: aws sqs receive-message shows an event whose detail.object.key is the file you uploaded. Cleanup: remove targets, delete the rule and turn the bucket setting off.

bash
aws s3api put-bucket-notification-configuration --bucket invicly-lab-uploads \
  --notification-configuration '{"EventBridgeConfiguration": {}}'
aws events put-rule --name uploads-to-work --event-pattern file://pattern.json
aws events put-targets --rule uploads-to-work \
  --targets Id=work,Arn=arn:aws:sqs:eu-west-1:<account-id>:files-work
# queue policy: allow sqs:SendMessage for events.amazonaws.com
#   with aws:SourceArn = arn:aws:events:eu-west-1:<account-id>:rule/uploads-to-work
aws s3 cp test.pdf s3://invicly-lab-uploads/uploads/test.pdf
aws sqs receive-message --queue-url <files-work-url> --wait-time-seconds 10

Trace one event to three consumers

A user uploads photo.jpg to uploads/. Step 1: S3 sends one Object Created event to the default bus. Step 2: rule A matches the uploads/ prefix and sends it to the processing queue. Step 3: rule B matches the .jpg suffix and sends it to the thumbnail queue. Step 4: rule C matches every object in the bucket and sends it to a CloudWatch Logs log group for audit. Step 5: the thumbnail worker is down; its queue grows, but processing and audit continue. When it is back, it drains its queue. Test the suffix rule offline first with test-event-pattern.

bash
aws events test-event-pattern \
  --event-pattern '{"source":["aws.s3"],"detail":{"object":{"key":[{"suffix":".jpg"}]}}}' \
  --event file://sample-object-created.json
# expect: {"Result": true}

Quick reference

  • Enable S3 to EventBridge with EventBridgeConfiguration {} on the bucket notification config
  • S3 events land on the default bus: source aws.s3, detail-type Object Created
  • Content filters: prefix, suffix, anything-but, numeric, exists
  • EventBridge rule: up to 5 targets; retries up to 24 hours; per-target DLQ
  • SNS filter policies match attributes by default; set FilterPolicyScope to MessageBody for JSON bodies
  • Every target queue needs a policy for the sending service, limited by aws:SourceArn
  • Test patterns with aws events test-event-pattern before deploying

Common mistakes

  • Creating the rule but forgetting to turn on EventBridge notifications for the bucket, so nothing arrives.
  • Pointing a rule or SNS subscription at an SQS queue whose access policy does not allow that service.
  • Sending events straight to a slow consumer with no queue, so a burst causes throttling and lost work.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1You created an EventBridge rule for S3 Object Created events, but nothing ever matches. What is the most likely cause?

Question 2

2Which service pushes one copy of each message to every subscriber?

Question 3

3Why put an SQS queue between EventBridge and a slow consumer?

Question 4

4Events reach the rule but not the SQS target, and FailedInvocations is rising. What should you check first?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Write an EventBridge pattern that matches Object Created events in bucket invicly-lab-uploads for keys ending in .pdf.

  2. Exercise 2medium

    Write the SQS queue policy statement that lets one EventBridge rule send messages to files-work.

  3. Exercise 3hard

    Choose SNS, EventBridge or direct S3 notifications for: (a) one Lambda processes every upload, (b) six teams subscribe to uploads with different key filters and want replay, (c) the same message must reach 50 SQS queues with minimal latency. Justify each.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I use SNS or EventBridge?
Use SNS for simple, fast fan-out of the same message to many subscribers. Use EventBridge when you route on event content, need many sources, or want archive and replay.
Does EventBridge cost money for S3 events?
AWS service events delivered to the default bus are free to publish, while custom events and some features are billed. Check the current EventBridge pricing page for your use.
Can SNS and EventBridge deliver an event twice?
Yes. Both deliver at least once, so consumers should be idempotent.

Go deeper

ShareXLinkedInWhatsApp