Processing Streams and Events with AWS Lambda
This lesson explains how AWS Lambda processes events from S3, streams, queues and schedules without servers, for lightweight, event-driven transformations and stream consumers.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how event source mappings poll streams and queues.
- Handle stream failures with bisecting, retries and on-failure destinations.
- Use tumbling windows for simple aggregations.
- Handle failures for asynchronous sources and SQS.
Event source mappings
For streams and queues, Lambda does the reading itself: an event source mapping polls Kinesis Data Streams, DynamoDB Streams, MSK, self-managed Kafka and SQS and invokes your function with batches of records. Several settings shape that behaviour. The batch size sets the most records per invocation, and the batching window lets Lambda wait up to a number of seconds to fill a batch, which means fewer, larger invocations at the cost of a little latency.
For streams, the starting position chooses where reading begins: LATEST for new records only, TRIM_HORIZON for the oldest record still retained, or a timestamp. Event filtering discards non-matching records before they reach the function, so you do not pay to invoke it for them. The parallelisation factor processes up to 10 batches from one shard at the same time, while still keeping records with the same partition key in order, which increases throughput without adding shards. The following table summarises the settings.
| Setting | Controls |
|---|---|
| Batch size | Records per invocation |
| Batching window | Wait up to N seconds to fill a batch |
| Starting position | LATEST, TRIM_HORIZON or a timestamp |
| Event filtering | Only matching records reach the function |
| Parallelisation factor | Up to 10 concurrent batches per shard, keeping order per partition key |
The following diagram shows an event source mapping filtering records and processing batches in parallel.

10batches
Processed at once per shard with the parallelisation factor
Event source mapping settings
Handling failures in streams
Records in a shard must be processed in order, so when a function fails on a batch from Kinesis or DynamoDB Streams, Lambda retries that batch and processes nothing newer from the shard until it succeeds or its records expire. One bad record can therefore stall a shard for days. Four settings prevent this. Bisect batch on function error splits a failing batch in two and retries each half, repeatedly, until the bad record is isolated. Maximum retry attempts and maximum record age stop retrying after a set number of attempts or once records are too old.
With ReportBatchItemFailures, the function returns the IDs of only the records that failed, so Lambda retries from the first failure instead of reprocessing the whole batch. An on-failure destination in SQS, SNS or S3 receives details of records that are finally given up on, so they can be examined and replayed rather than lost. The following table summarises the settings.
| Setting | Effect |
|---|---|
| Bisect batch on function error | Splits the batch to isolate the bad record |
| Maximum retry attempts, maximum record age | Limit retries |
| ReportBatchItemFailures | Returns only the failed record IDs, so only they are retried |
| On-failure destination (SQS, SNS, S3) | Receives records that cannot be processed |
The following diagram shows the batch being split until the bad record is isolated.

A stalled shard
Tumbling windows
Tumbling windows of up to 15 minutes let a Kinesis or DynamoDB Streams consumer keep state within a window, for simple aggregations such as counts per minute. For complex stateful processing, use Managed Service for Apache Flink.
Asynchronous sources and SQS
Other sources handle failures differently. Amazon S3, SNS and EventBridge invoke functions asynchronously: Lambda places the event in an internal queue and, if the function fails, retries it twice more. After that, the event goes to a dead-letter queue or an on-failure destination if one is configured, and is otherwise discarded, so configuring one is essential for pipelines that must not lose files.
With SQS, a message that fails processing is not deleted; it becomes visible in the queue again after its visibility timeout and is retried. After the queue's maximum number of receives, SQS moves it to the queue's own dead-letter queue (DLQ). The following table summarises the behaviour.
| Source | Failure handling |
|---|---|
| S3 events, SNS, EventBridge (asynchronous) | Lambda retries twice, then sends to a dead-letter queue or on-failure destination |
| SQS | Failed messages return after the visibility timeout and move to the queue's DLQ after the maximum receives |
The following diagram compares the failure paths for asynchronous sources and for SQS.

Asynchronous failures
A stream consumer stuck on one record
A Lambda function consuming a Kinesis stream stops making progress, and the IteratorAge metric climbs for hours. One malformed record makes the function fail, and Lambda retries the same batch forever, blocking the shard.
| Setting | Value | Effect |
|---|---|---|
| Maximum retry attempts | 3 | Stops retrying a failing batch forever |
| Bisect batch on function error | On | Splits the batch to isolate the bad record |
| On-failure destination | An SQS queue | Keeps details of skipped records |
| Report batch item failures | On | Retries only from the failed record onwards |
The shard resumes, the bad record is captured for investigation, and good records are no longer held up behind it.
How can a Lambda function retry only the failed records of a batch?
Show answer
By returning the failed records with ReportBatchItemFailures, so Lambda retries from the first failed record rather than the whole batch.
Exam Tips
Remember for the exam
- To increase throughput per Kinesis shard, raise the parallelisation factor, up to 10.
- To retry only the failed records in a batch, use ReportBatchItemFailures.
- When one bad record blocks a shard, combine bisect on error, a maximum retry count and an on-failure destination.
- For a simple windowed aggregation in Lambda, use tumbling windows.
- To process only matching records, add event filtering to the event source mapping.