Skip to content

How do I troubleshoot DynamoDB Streams in my Lambda functions?

5 minute read
1

I want to troubleshoot Amazon DynamoDB Streams with my AWS Lambda functions.

Resolution

DynamoDB stream didn't invoke the function

ARN mismatch

If your event source mapping (ESM) stops invoking your function, then check for a stream Amazon Resource Name (ARN) mismatch issue. If you deactivate and reactivate DynamoDB Streams on your table, then DynamoDB generates a new stream ARN. Your existing ESM still references the old stream ARN and silently stops processing records. 

To resolve this issue, compare the current stream ARN on your table with the ESM's configured event source ARN. If they don't match, then delete the old ESM and create a new one that points to the current stream ARN.

Failed batch requests

Check for a failed batch stuck in a loop. With the default settings, the ESM retries a failed batches indefinitely and then blocks the shard. Configure MaximumRetryAttempts and BisectBatchOnFunctionError to split the batch in two and retry the request.

Cross-accounts

For cross-account configurations, confirm that the Lambda function's execution role has valid permissions to control access to streams. Confirm that the execution role has permissions to dynamodb:DescribeStream, dynamodb:GetRecords, dynamodb:GetShardIterator, and dynamodb:ListStreams permissions on the stream in the other account. Also, make sure that the stream's resource-based policy allows access to the execution role.

For more information, see Simplify cross-account stream processing with AWS Lambda and Amazon DynamoDB.

Control data processing from the DynamoDB stream

Use batch window and batch size to control how Lambda delivers records to your function.The batch window sets the maximum number of seconds that Lambda spends gathering records before invoking the function. The maximum batch window value is 300 seconds. The batch size sets the maximum number of records in the batch with a maximum value of 1,0000.

Lambda invokes your function when:

  • The payload size reaches 6 MB.
  • The batch window reaches its configured value.
  • The batch size reaches its configured value.

To filter the records that invoke your function, use event filtering with a DynamoDB event source. For example, to process only INSERT and MODIFY events and skip DELETE events from TTL expirations, add a filter pattern to your ESM.

For more information, see Control which events Lambda sends to your function.

Use Parallelization factor to speed up data processing

When you turn on a DynamoDB stream on a DynamoDB table, DynamoDB associates one shard for each partition. For example, if your DynamoDB table has 10 partitions with DynamoDB Streams turned on, then you have 10 shards.

If the number of partitions in your table increases, then the number of shards in the stream also increases.

Each partition on a DynamoDB table can handle up to 3,000 read capacity units (RCUs), 1,000 write capacity units (WCUs), and 10 GB of data. If you exceed these parameters, then DynamoDB creates a new partition and a new shard in the stream.

To improve stream processing, configure your function's parallelization factor to increase the number of concurrent Lambda invocations for each shard of a stream. Specify the number of concurrent batches that Lambda polls from each shard.

A parallelization factor of 10 across 5 data shards gives you up to 50 concurrent Lambda invocations.

For example, concurrent runs = number of shards x concurrent batches per shard.

For more information, see Why does it take so long to process my DynamoDB streams?

Configure error handling

Configure error handling and retries settings for Lambda invocations.

If the Lambda function fails and you have BisectBatchonFunctionError turned on, then Lambda splits the batches into two.

Lambda retries the split batches until it isolates the problem record. The MaximumRetryAttempts and MaximumRecordAgeInSeconds settings control how Lambda processes retries.

If you set MaximumRetryAttempts to 0, then Lambda discards failed records or sends them to the on-failure destination.

Note: It's a best practice to add an on-failure destination to retain records of failed event source mapping invocations. The destination receives batch metadata including the shard ID and sequence numbers. Use the sequence numbers to retrieve original records within the 24 hour stream retention window.

Example 1

In the following example, p is the problem record and MaximumRetryAttempts set to 0.

Batch record: [1, 2, 3p, 4, 5p]

Split 1: [3p, 4, 5p]

Split 2: [3p] [4, 5p] The Lambda retry discards [3p] because it's identified as the problem record or sends it to the on-failure destination.

Split 3: Lambda processes [4] and either discards [5p] or sends it to the on-failure destination.

Example 2

In the following example, x is the problem record and MaximumRetryAttempts set to -1 infinite.

Batch records: [1, 2, 3x, 4, 5x]

The [3x, 4, 5x] batch record retries indefinitely and blocks the shard.

For more information, see Understanding retry behavior in Lambda.

Prevent duplicate Lambda events

DynamoDB Streams provides at-least-once delivery. Duplicate invocations might occur during shard splits, Lambda service retries, or function timeouts mid-batch. To prevent duplicate Lambda events, you must design your function logic to correctly handle duplicated events.

For more information, see How do I prevent duplicate Lambda events?

Decrease function IteratorAge metric

The Lambda IteratorAge metric measures the latency between when DynamoDB adds a record to the stream and when the function processes that record.

An increase in IteratorAge means that Lambda isn't efficiently processing records written to the DynamoDB stream.

To resolve this issue, increase parallelization factor, reduce batch size, filter out unnecessary records, and configure retry limits with an on-failure destination.

For more information, see Why is the Lambda IteratorAge metric increasing for my DynamoDB stream? and How do I decrease my Lambda functions IteratorAge metric?

Related information

Using AWS Lambda with Amazon DynamoDB

DynamoDB Streams and AWS Lambda triggers

Why are records missing in my DynamoDB streams?

AWS OFFICIALUpdated 3 months ago