How I Use DynamoDB Streams & Lambda To Create Event-Driven Apps
The leanest and simplest way to create event-driven applications.
What is the fastest way to build web apps today?
I remember only 10 years ago building web applications required setting up complex infrastructure and having the necessary expertise.
No wonder it was so much easier to work in the frontend stack.
Today much of these complexities have been abstracted away and managed for you, allowing you to build serverless systems in minutes instead of days or weeks.
One of the fastest, and simultaneously, most scalable ways to build apps today is by using the AWS cloud.
Specifically I use two services to rapidly build and deploy proof of concepts: AWS Lambda and Amazon DynamoDB.
One powerful feature DynamoDB has is called “streams” and they help you build event-driven microservices very easily.
What Are DynamoDB Streams?
DynamoDB streams is a feature that captures changes you make to an item in your DynamoDB table.
Whenever a PutItem, UpdateItem or DeleteItem operation is made on your table, a stream records these changes. With these streams you can trigger consequent events or processes.
Streams enable you to design powerful event-driven architectures. Let’s see how.
Design Event-Driven Architectures With Streams
DynamoDB streams allow microservices and applications to respond to changes in a DynamoDB table.
By capturing these changes, you can react to these change “events” or records and trigger a Lambda function in response.
For example, I’ve used streams to send notifications to users when someone comments on their posts.
Using the PutItem change event of a given comment, you can easily craft a notification object with the comment’s content, date, author’s userID, and more relevant information and send it out as a notification to the post author.
Another classic example is to send out an email or notification to a user when some important activity occurs on their account. Adding a new notification item of the activity to the DynamoDB table, can then trigger a Lambda function to send out an email or publish a notification to subscribers.
Streams use cases are limitless and allow for scalable and responsive event-driven applications. Let’s take a look at one below.
Building an event-driven microservice with DynamoDB streams and Lambda
Setting up DynamoDB streams and integrating them with Lambda is a super quick and easy process.
There are 3 simple steps involved in doing this:
Enabling streams on your DynamoDB table.
Creating a Lambda function and writing the code to accept stream events.
Connecting the Lambda function with the DynamoDB stream.
DynamoDB Table Setup
In the DynamoDB console, create a new table called “users”.
Once created, head over to the Exports and streams tab.
Scrolling down you’ll find the section DynamoDB stream details. Click on the Turn on button.
Here select the New image option to capture the entire item so we can get the necessary information we need from the change event.
Click on the Turn On stream button to enable the stream.
Lambda Function Setup
In the AWS Lambda console, create a new function.
Use the following configurations:
Choose Author from scratch option.
name it “stream-test”.
Use the Node JS 20.x runtime.
In the permissions section, add a role with permissions to DynamoDB. (learn how to do this here.)
Once the function is created, scroll down to the Code section below and add the following code:
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
const snsClient = new SNSClient({ region: "us-east-1" });
const SNS_TOPIC_ARN = <your_ARN_SNS_Topic>; //create an SNS topic for this
export const handler = async (event) => {
for (const record of event.Records) {
if (record.eventName === 'INSERT') {
const newUser = record.dynamodb.NewImage;
const message = `New user added: ${JSON.stringify(newUser)}`;
const params = {
Message: message,
TopicArn: SNS_TOPIC_ARN,
};
try {
const data = await snsClient.send(new PublishCommand(params));
console.log(`Message sent to SNS: ${data.MessageId}`);
} catch (err) {
console.error("Error sending SNS message:", err);
}
}
}
};
The code above ingests stream events from DynamoDB.
When a insert event type is performed (a PutItem) on the connected DynamoDB, the Lambda code will immediately send an SNS message to all subscribers.
The code is simple yet effective.
Don’t forget to hit Deploy to save and deploy the serverless function.
Integrate The Stream With Lambda
Lastly, let’s connect this Lambda function with our DynamoDB stream.
At the top of the Lambda function page, add a trigger by clicking on the Add trigger button:
On the new page, from the list of trigger, choose DynamoDB.
Choose the users table we created earlier and click on the Add button.
You should see a message saying the DynamoDB trigger was added successfully.
Now all you need to do is add an item to your users table and the SNS message will be sent (assuming you have an existing SNS topic created).
Conclusion
With only two or three services — DynamoDB, Lambda and optionally SNS — we can create powerful event-driven microservices at scale.
These systems can scale effortlessly as they’re built on top of three inherently scalable services.
Creating and using DynamoDB streams is a super quick and easy process and allows you to accomodate all sorts of event-driven processes in minutes.
📖 P.S. I'm creating a free email course on DynamoDB that will teach you concepts and design patterns not taught in any existing course out there. Signup for free here:
https://www.buildawaitlist.com/waitlist/dynamodb-course
👋 My name is Uriel Bitton and I’m committed to helping you master Serverless, Cloud Computing, and AWS.
🚀 If you want to learn how to build serverless, scalable, and resilient applications, you can also follow me on Linkedin for valuable daily posts.
Thanks for reading and see you in the next one!