-
Notifications
You must be signed in to change notification settings - Fork 0
trigger lambda with dynamodb stream
Delvin Defoe edited this page Jan 17, 2024
·
1 revision
There are multiple ways to trigger or invoke your AWS Lambda function. You can invoke your function directly using
- the AWS Lambda console
- a function URL HTTP(S) endpoint
- the Lambda API
- an AWS SDK
- the AWS Command Line Interface (AWS CLI)
- AWS toolkits
You can also configure Lambda to read items from a DynamoDB stream and invoke your function to process them. This is what we will focus on in this article.
Consider the following code snippet.
import { DynamoEventSource } from 'aws-cdk-lib/aws-lambda-event-sources';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as lambda from 'aws-cdk-lib/aws-lambda';
// Provision your lambda function and complete its configuration
declare const lambdaFunction : lambda.Function;
// Provision your DynamoDb table with stream
const catalogTable = new dynamodb.Table(this, 'CatalogTable', {
partitionKey: {
name: 'id',
type: dynamodb.AttributeType.STRING,
},
stream: dynamodb.StreamViewType.NEW_IMAGE,
});
// Setup filter criteria for Lambda event filtering
const filterCriteria = {
dynamodb: {
NewImage: {
// filter criteria to process records with given author
author: {
S: lambda.FilterRule.isEqual('Author One')
},
completionDateTime: {
N: lambda.FilterRule.notExists()
} // AND no completion date time
}
}
};
// Add event source to trigger your function
lambdaFunction.addEventSource(DynamoEventSource(table, {
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
bisectBatchOnError: true,
batchSize: 100,
reportBatchItemFailures: true,
parallelizationFactor: 10,
filters: [lambda.FilterCriteria.filter(filterCriteria)],
}));