Building Event Processing Workflows with SQS, SNS, Kinesis, Lambda and Step Functions
Between an event source and the action it triggers, workflows need buffering, fan-out, ordering, retries and orchestration. This lesson explains the messaging and streaming services that build these workflows, Amazon SQS, Amazon SNS and Amazon Kinesis Data Streams, how Lambda and Step Functions process events from them, and the patterns used to make event processing reliable and scalable.
Learning outcomes
After completing this lesson, you will be able to:
- Explain SQS standard and FIFO queues, visibility timeout, long polling and dead-letter queues.
- Explain SNS topics, subscription filter policies, FIFO topics and the SNS-to-SQS fan-out pattern.
- Choose between SQS, SNS, Kinesis Data Streams and EventBridge for an event processing requirement.
- Design reliable workflows that handle failures, ordering, duplicates and load spikes.
Amazon SQS
Amazon SQS is a fully managed message queue that decouples producers from consumers. Standard queues offer nearly unlimited throughput with at-least-once delivery and best-effort ordering. FIFO queues preserve order within a message group and provide exactly-once processing within a deduplication interval of five minutes, with high but limited throughput, which high throughput mode raises.
When a consumer receives a message, the message becomes invisible for the visibility timeout (default 30 seconds, up to 12 hours). If the consumer deletes the message within that time, processing is complete; otherwise the message becomes visible again and is retried. The visibility timeout should exceed the processing time; for Lambda event source mappings, AWS recommends at least six times the function timeout. Long polling (receive wait time up to 20 seconds) reduces empty responses and cost. A dead-letter queue receives messages that have been received more than the maxReceiveCount of the redrive policy, isolating poison messages, and redrive moves them back to the source queue after the problem is fixed. Messages are retained for up to 14 days.
Queues provide load levelling: a burst of events is absorbed by the queue and processed at the rate consumers can sustain, protecting databases and APIs. The ApproximateAgeOfOldestMessage metric shows whether consumers keep up, and the backlog per worker drives consumer scaling, as described in the Auto Scaling lesson.
Reliable event processing
Amazon SNS
Amazon SNS is a publish-subscribe service. A message published to a topic is pushed to every subscription: SQS queues, Lambda functions, HTTPS endpoints, Firehose, email, SMS and mobile push. Subscription filter policies let each subscriber receive only messages whose attributes or body match, so one topic can serve consumers interested in different message types. FIFO topics deliver to FIFO queues in order with deduplication. Subscriptions can have their own dead-letter queues for messages that cannot be delivered, and delivery retry policies apply to HTTP endpoints. Topics can be encrypted with KMS, which requires publishers such as CloudWatch alarms or EventBridge to be allowed to use the key.
The fan-out pattern
Publishing to an SNS topic with several SQS queue subscriptions delivers each message durably to every consumer, and each consumer processes its queue at its own pace with its own retries and DLQ. This SNS-to-SQS fan-out pattern is the standard way to send the same event to several independent processing workflows, such as an order event that triggers billing, shipping and analytics.
Amazon Kinesis Data Streams in Event Workflows
Kinesis Data Streams, introduced in the log analytics lesson, suits event workflows that need strict ordering per key at high throughput, multiple consumers reading the same data independently, and replay for up to 365 days. Unlike SQS, consumed records are not deleted, so a new consumer can reprocess history. Unlike SNS, consumers pull at their own pace. Lambda processes shards in order, and errors block a shard until resolved unless bisect-on-error, retry limits and failure destinations are configured.
Choosing a Service
The services overlap, so the choice depends on the decisive requirement. They are compared below with EventBridge.
Events from software as a service (SaaS) applications reach these workflows in two ways. Partners such as Zendesk, PagerDuty and Datadog publish events to an EventBridge partner event source, which an account associates with a partner event bus. Amazon AppFlow transfers data in bulk between SaaS applications, such as Salesforce or ServiceNow, and AWS services such as S3, on a schedule, on demand or when a SaaS event occurs. It suits moving records, such as incident tickets, into S3 for analysis with Athena. EventBridge is for reacting to individual events; AppFlow is for moving data.
Choose the messaging service
Orchestration and Reliability Patterns
Lambda processes events from all of these sources with the invocation behaviours described in the automation module, and Step Functions orchestrates multi-step responses. Several patterns make event-driven workflows reliable.
- Idempotent consumers: all of these services can deliver a message more than once, so processing must tolerate duplicates, for example by recording processed IDs.
- Dead-letter queues everywhere: on SQS queues, SNS subscriptions, EventBridge targets and Lambda asynchronous invocations, with alarms on their depth.
- Buffer before fragile systems: place a queue in front of databases and third-party APIs, and limit consumer concurrency.
- Orchestrate multi-step responses: start a Step Functions workflow from EventBridge or SQS when a response involves several steps, waits, approvals or compensation.
- Preserve order only where needed: FIFO queues and Kinesis add constraints and cost.
- Observe the flow: monitor queue age, iterator age, failed invocations and DLQ depth, and trace with X-Ray where supported.
Processing Security Findings at Scale
A security team wants every high-severity finding from GuardDuty and Security Hub across the organisation to be: posted to a chat channel, recorded in a ticketing system whose API accepts 5 requests per second, and, for compromised instances, handled by an automated isolation workflow. Bursts of hundreds of findings must not overload the ticketing API.
-
Central routing
Findings arrive on a central EventBridge bus from all accounts; a rule matches high severity and targets an SNS topic.
-
Fan-out
The topic delivers to Amazon Q Developer in chat applications, to an SQS queue for tickets and, through a filter policy on resource type, to a second queue for instance findings.
-
Load levelling
A Lambda function consumes the ticket queue with event source mapping maximum concurrency set to keep within the ticketing API's limit, with a DLQ and alarm.
-
Orchestration
Instance findings start a Step Functions workflow that isolates the instance, snapshots volumes and waits for analyst approval before termination.
A Lambda function processing an SQS queue takes up to 4 minutes per message, and messages are processed twice. The queue's visibility timeout is 30 seconds. What is wrong?
Show answer
Messages become visible again after 30 seconds while still being processed and are delivered to another invocation. Set the visibility timeout well above the processing time; AWS recommends at least six times the function timeout.
Exam Tips
Remember for the exam
- SQS: standard (at least once) or FIFO (ordered, deduplicated); visibility timeout above processing time (six times the Lambda timeout); DLQs with maxReceiveCount; retention up to 14 days.
- SNS: push to many subscribers with filter policies; SNS-to-SQS fan-out gives each consumer a durable queue.
- Kinesis Data Streams: ordered per shard, multiple consumers and replay; EventBridge: content-based routing with archives.
- Design for duplicates, add DLQs and alarms everywhere, buffer fragile systems and orchestrate multi-step responses with Step Functions.