Skip to content
v2.274.0

DynamoEventSource Class ​

Use an Amazon DynamoDB stream as an event source for AWS Lambda.

Example ​

ts
import * as eventsources from 'aws-cdk-lib/aws-lambda-event-sources';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';

declare const fn: lambda.Function;
const table = new dynamodb.Table(this, 'Table', {
  partitionKey: {
    name: 'id',
    type: dynamodb.AttributeType.STRING,
  },
  stream: dynamodb.StreamViewType.NEW_IMAGE,
});

fn.addEventSource(new eventsources.DynamoEventSource(table, {
  startingPosition: lambda.StartingPosition.LATEST,
  metricsConfig: {
    metrics: [lambda.MetricType.EVENT_COUNT],
  }
}));
python
import aws_cdk.aws_lambda_event_sources as eventsources
import aws_cdk.aws_dynamodb as dynamodb

# fn: lambda.Function

table = dynamodb.Table(self, "Table",
    partition_key=dynamodb.Attribute(
        name="id",
        type=dynamodb.AttributeType.STRING
    ),
    stream=dynamodb.StreamViewType.NEW_IMAGE
)

fn.add_event_source(eventsources.DynamoEventSource(table,
    starting_position=lambda_.StartingPosition.LATEST,
    metrics_config=lambda.MetricsConfig(
        metrics=[lambda_.MetricType.EVENT_COUNT]
    )
))
java
import software.amazon.awscdk.services.lambda.eventsources.*;
import software.amazon.awscdk.services.dynamodb.*;

Function fn;

Table table = Table.Builder.create(this, "Table")
        .partitionKey(Attribute.builder()
                .name("id")
                .type(AttributeType.STRING)
                .build())
        .stream(StreamViewType.NEW_IMAGE)
        .build();

fn.addEventSource(DynamoEventSource.Builder.create(table)
        .startingPosition(StartingPosition.LATEST)
        .metricsConfig(MetricsConfig.builder()
                .metrics(List.of(MetricType.EVENT_COUNT))
                .build())
        .build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.Lambda.EventSources;
using Amazon.CDK.AWS.DynamoDB;

Function fn;

var table = new Table(this, "Table", new TableProps {
    PartitionKey = new Attribute {
        Name = "id",
        Type = AttributeType.STRING
    },
    Stream = StreamViewType.NEW_IMAGE
});

fn.AddEventSource(new DynamoEventSource(table, new DynamoEventSourceProps {
    StartingPosition = StartingPosition.LATEST,
    MetricsConfig = new MetricsConfig {
        Metrics = new [] { MetricType.EVENT_COUNT }
    }
}));
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import eventsources "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

var fn Function

table := dynamodb.NewTable(this, jsii.String("Table"), &TableProps{
	PartitionKey: &Attribute{
		Name: jsii.String("id"),
		Type: dynamodb.AttributeType_STRING,
	},
	Stream: dynamodb.StreamViewType_NEW_IMAGE,
})

fn.AddEventSource(eventsources.NewDynamoEventSource(table, &DynamoEventSourceProps{
	StartingPosition: lambda.StartingPosition_LATEST,
	MetricsConfig: &MetricsConfig{
		Metrics: []MetricType{
			lambda.MetricType_EVENT_COUNT,
		},
	},
}))

Initializer ​

ts
new DynamoEventSource(table: ITable, props: DynamoEventSourceProps)
python
DynamoEventSource(table: ITable, props: DynamoEventSourceProps)
java
new DynamoEventSource(ITable table, DynamoEventSourceProps props)
csharp
new DynamoEventSource(ITable table, DynamoEventSourceProps props)
go
NewDynamoEventSource(Table ITable, Props DynamoEventSourceProps)

Properties ​

eventSourceMappingArnstringevent_source_mapping_arnstreventSourceMappingArnStringEventSourceMappingArnstringEventSourceMappingArn*stringrequired#
The ARN for this EventSourceMapping.
eventSourceMappingIdstringevent_source_mapping_idstreventSourceMappingIdStringEventSourceMappingIdstringEventSourceMappingId*stringrequired#
The identifier for this EventSourceMapping.

Methods ​

bind(target: IFunction): voiddef bind(self, target: IFunction) -> Nonepublic void bind(IFunction target)public void Bind(IFunction target)func Bind(Target IFunction)#
Called by `lambda.addEventSource` to allow the event source to bind to this function.