# Exercise 2: SNS → SQS Fan-Out With Filtering

`orders-service` publishes `OrderPlaced`, `OrderCancelled` and `OrderShipped` events to SNS topic `order-events` with a message attribute `type`. Subscribers:

- **billing** — needs `OrderPlaced` and `OrderCancelled`
- **email** — needs all three
- **analytics** — needs all, delivered raw (no SNS envelope), batched into S3 by a Lambda
- **warehouse** — only `OrderPlaced` for orders where `region` = `eu`

## Deliverables

1. CLI commands to create the topic, the four queues (each with its own DLQ), and the four subscriptions with **raw message delivery** and the correct **filter policies**.
2. The **SQS access policy** (JSON) that allows the topic to send to a queue — and explain what breaks if it's missing.
3. The publish command for an `OrderPlaced` EU order with both message attributes.
4. Explain why each service gets its own queue instead of all four sharing one, in terms of pace, retries and failures.
5. The billing team says: "Sometimes we see the same `OrderPlaced` twice." Explain why, and what billing must do.
6. Analytics wants all events in the order they happened per order. Can SNS Standard + SQS Standard give that? What would you change?

## Requirements Checklist

- [ ] Filter policies: billing `{"type":["OrderPlaced","OrderCancelled"]}`, email none (or all three), warehouse `{"type":["OrderPlaced"],"region":["eu"]}` (both attributes must match), analytics none.
- [ ] `RawMessageDelivery=true` on subscriptions where consumers expect the original body.
- [ ] Queue policy: `Effect: Allow`, `Principal: {"Service":"sns.amazonaws.com"}`, `Action: sqs:SendMessage`, `Resource: <queue arn>`, `Condition: {"ArnEquals":{"aws:SourceArn":"<topic arn>"}}`; without it, deliveries fail silently from the publisher's point of view.
- [ ] Publish includes `--message-attributes` for `type` and `region`.
- [ ] Per-service queues isolate slow consumers, retries, DLQs and scaling.
- [ ] Duplicates: at-least-once delivery → idempotent consumer keyed by event/order id.
- [ ] Ordering: requires SNS FIFO topic → SQS FIFO queues with `MessageGroupId = orderId`.
