0
0
DynamodbHow-ToBeginner ยท 4 min read

How to Use DynamoDB Streams with Lambda for Real-Time Processing

To use DynamoDB Streams with AWS Lambda, enable streams on your DynamoDB table and create a Lambda function triggered by the stream. This setup lets Lambda automatically run your code whenever data changes occur in the table, enabling real-time processing.
๐Ÿ“

Syntax

Here is the basic syntax to connect a DynamoDB stream to a Lambda function:

  • EnableStreamSpecification: Turn on streams on your DynamoDB table with a view type (e.g., NEW_IMAGE).
  • EventSourceMapping: Create a mapping that triggers your Lambda function when new stream records arrive.
  • LambdaHandler: Your Lambda function code processes the stream event records.
bash
aws dynamodb update-table --table-name YourTableName --stream-specification StreamEnabled=true,StreamViewType=NEW_IMAGE

aws lambda create-event-source-mapping --function-name YourLambdaFunction --event-source-arn arn:aws:dynamodb:region:account-id:table/YourTableName/stream/timestamp --starting-position TRIM_HORIZON
๐Ÿ’ป

Example

This example shows a simple AWS Lambda function in Node.js that processes DynamoDB stream events and logs the changed items.

javascript
exports.handler = async (event) => {
  for (const record of event.Records) {
    console.log('Event ID:', record.eventID);
    console.log('Event Name:', record.eventName);
    if (record.dynamodb.NewImage) {
      console.log('New item:', JSON.stringify(record.dynamodb.NewImage));
    }
  }
  return `Processed ${event.Records.length} records.`;
};
Output
Event ID: 1 Event Name: INSERT New item: {"id":{"S":"123"},"name":{"S":"Alice"}} Processed 1 records.
โš ๏ธ

Common Pitfalls

Common mistakes when using DynamoDB Streams with Lambda include:

  • Not enabling streams on the DynamoDB table before creating the event source mapping.
  • Choosing the wrong StreamViewType that does not include the data you need.
  • Not setting the correct IAM permissions for Lambda to read from the stream.
  • Ignoring batch size and retry settings that affect processing performance.

Always verify your stream ARN and Lambda permissions carefully.

json
/* Wrong: Lambda without permission to read stream */
// This causes an error when Lambda tries to access the stream

/* Right: Attach this IAM policy to Lambda role */
{
  "Effect": "Allow",
  "Action": ["dynamodb:DescribeStream", "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:ListStreams"],
  "Resource": "arn:aws:dynamodb:region:account-id:table/YourTableName/stream/*"
}
๐Ÿ“Š

Quick Reference

StepDescription
Enable StreamsTurn on streams on your DynamoDB table with desired view type.
Create LambdaWrite a Lambda function to process stream events.
Set Event Source MappingLink DynamoDB stream ARN to Lambda function.
Configure PermissionsGive Lambda permission to read from DynamoDB streams.
Test & MonitorInsert/update/delete items and check Lambda logs.
โœ…

Key Takeaways

Enable DynamoDB Streams on your table before linking to Lambda.
Create an event source mapping to trigger Lambda from stream events.
Ensure Lambda has proper IAM permissions to access the stream.
Choose the right StreamViewType to get needed data in events.
Test your setup by making changes to the DynamoDB table and checking Lambda logs.