Lesson 1 of 3 AWS Lambda for Data Engineering

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.

SettingControls
Batch sizeRecords per invocation
Batching windowWait up to N seconds to fill a batch
Starting positionLATEST, TRIM_HORIZON or a timestamp
Event filteringOnly matching records reach the function
Parallelisation factorUp 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.

A Lambda event source mapping reading one shard. Event filtering drops non-matching records before any invocation. With a parallelisation factor of three, the mapping processes three batches from the shard at the same time, and records with the same partition key always go to the same batch, so they stay in order.
The records and keys are illustrative.

10batches

Processed at once per shard with the parallelisation factor

Match the pairs

Event source mapping settings

Exam skills 1.1.7 · 1.4.2

Match each requirement with the event source mapping setting.

Log in to try this activity Interactive activities are free with an account.

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.

SettingEffect
Bisect batch on function errorSplits the batch to isolate the bad record
Maximum retry attempts, maximum record ageLimit retries
ReportBatchItemFailuresReturns 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.

Bisect batch on function error isolating a malformed record. The failing batch of records 1 to 8 is split in two, and the failing half is split again until record 7 is alone. After the maximum retry attempts, its details go to the on-failure SQS queue and the shard resumes; ReportBatchItemFailures retries only from the failed record onwards.
The record numbers are illustrative.
Scenario questions

A stalled shard

Exam skills 1.1.7 · 3.3.6

Choose the setting for each stream failure.

Log in to try this activity Interactive activities are free with an account.

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.

SourceFailure handling
S3 events, SNS, EventBridge (asynchronous)Lambda retries twice, then sends to a dead-letter queue or on-failure destination
SQSFailed 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.

How failures are handled for asynchronous sources and for SQS. For S3, SNS and EventBridge, Lambda retries a failed event twice and then sends it to a dead-letter queue or on-failure destination, or discards it if none is configured. With SQS, a failed message reappears after the visibility timeout and moves to the queue's dead-letter queue after the maximum number of receives.
DLQ is a dead-letter queue.
True or false

Asynchronous failures

Exam skills 1.1.6 · 3.1.8

Decide whether each statement about Lambda failure handling is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

SettingValueEffect
Maximum retry attempts3Stops retrying a failing batch forever
Bisect batch on function errorOnSplits the batch to isolate the bad record
On-failure destinationAn SQS queueKeeps details of skipped records
Report batch item failuresOnRetries 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.

Check yourself

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.