How to capture Table changes with DynamoDB Streams & Lambda with AWS CDK

I am a french Developer, working for a french startup :) i like video games, computers and drinking Ricoré
Search for a command to run...

I am a french Developer, working for a french startup :) i like video games, computers and drinking Ricoré
Thanks Flo ! U are the best !
AWS Bedrock AgentCore lets you deploy agents as managed containerized runtimes. This guide shows how to automate this deployment using Pulumi and Docker. What You'll Deploy Docker container with your agent code Amazon ECR to store the image Bedroc...

Introduction This guide shows how to deploy AWS Bedrock Agents using Pulumi and TypeScript. Prerequisites AWS Account with Bedrock access Node.js and npm installed Pulumi CLI installed Project Setup Generate Project with Pulumi pulumi new aws-t...

Starting My Homelab Journey with a Raspberry Pi 4B

I recently discovered Docker compose profiles and wanted to share it as it can be useful to someone else. Docker Compose profiles let you control which services run without modifying your docker-compose.yml. They’re useful for separating optional s...

These days, i’m using AWS Bedrock to create Agents.And i faced this error when testing my agents many times so i decided to write down the workaround that i found. An error occurred (dependencyFailedException) when calling the InvokeAgent operation: ...

What is a stream ?
According to the AWS Documentation :
A DynamoDB stream is an ordered flow of information about changes to items in a DynamoDB table. Whenever an application creates, updates, or deletes items in the table, DynamoDB Streams writes a stream record with the primary key attributes of the items that were modified
According to the definition, every time a change happens in a DynamoDB table, it is stored in a stream.
Okay, but what can we do with that stream ?
Well, you can use a stream to invoke a Lambda function.

For example, you have an application that creates new Users on a DynamoDB table.
It could be useful to send to automatically an email to your new users.
You can do that with DynamoDB streams.
Let's create the main folder of the project
mkdir streamLambdaExample
cd streamLambdaExample
npm init -y
Then install CDK and aws-lambda package
npm install -g aws-cdk
npm i @aws-cdk/aws-lambda
npm i @aws-cdk/aws-dynamodb
npm i @aws-cdk/aws-lambda-event-sources
mkdir cdk-folder
cdk init app --language typescript
#### CDK part
Open the file lib/cdk-folder-stack.ts
import * as cdk from '@aws-cdk/core';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as lambda from '@aws-cdk/aws-lambda';
import * as path from 'path';
import { DynamoEventSource } from '@aws-cdk/aws-lambda-event-sources';
export class CdkFolderStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const usersTable = new dynamodb.Table(this, 'Users', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
stream: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES,
tableName: 'Users',
});
const myLambdaFunction = new lambda.Function(this, 'my-lambda', {
runtime: lambda.Runtime.NODEJS_14_X,
memorySize: 256,
timeout: cdk.Duration.seconds(60),
handler: 'index.main',
code: lambda.Code.fromAsset(path.join(__dirname, '/../app/my-lambda/')),
});
myLambdaFunction.addEventSource(new DynamoEventSource(usersTable, {
startingPosition: lambda.StartingPosition.LATEST,
}));
}
}
Now the lambda code
mkdir app
cd app
mkdir my-lambda
cd my-lambda
npm init -y
touch index.js
We are just going to log the DynamoDB record.
exports.main = async (event) => {
for (const record of event.Records){
console.log('DynamoDB Record: %j', record.dynamodb);
}
};
Let's bootstrap first
cdk bootstrap
⏳ Bootstrapping environment aws://XXXXXXXXXXXXX/us-east-1...
✅ Environment aws://XXXXXXXXXXXXXX/us-east-1 bootstrapped (no changes).
Second command, to create the Cloudformation template for our app :
cdk synth
And now, let's deploy !
cdk deploy
First, let's see our DynamoDB table
Go to DynamoDB on the console (on the right region), to see your Users table :

We are going to create a User on the table (click on Create Item)

Fill with the info you want :

Hit save.
If we configured everything correctly, our lambda should be triggered and display the log.
Go to the Lambda console (you can see that the trigger is DynamoDB) :

Go to the Cloudwatch Logs of your Lambda :

Your lambda has been triggered by the DynamoDB stream ! Well done !
Don't forget to clean after this tutorial, i don't want you to have a huge bill on AWS!
cdk destroy
Fell free to ask me questions in the comments ! See you in the next one ! 🤖