← Home

DynamoDB Stream to Lambda Filtering With Go CDK

It’s been bothering me that I have a Lambda function set up to process data from DynamoDB Streams that doesn’t use Stream event filtering.

All of the filtering was done in Lambda itself, wasting precious CPU time.

Since v2.42.0 [0] of CDK it’s been possible to use Lambda function filtering, but I hadn’t looked into it.

In TypeScript, the syntax looks fairly sensible.

lambda.FilterCriteria.filter({
	eventName: lambda.FilterRule.isEqual("INSERT"),
	dynamodb: { NewImage: { "_pk": { S: lambda.FilterRule.beginsWith("OUTBOUND/") } } },
}),

In Go, it very much does not:

filters := []*map[string]any{
	awslambda.FilterCriteria_Filter(&map[string]any{
		"eventName": awslambda.FilterRule_IsEqual("INSERT"),
		"dynamodb": &map[string]any{
			"NewImage": &map[string]any{
				"_sk": &map[string]any{
					"S": awslambda.FilterRule_BeginsWith(jsii.String("OUTBOUND/")),
				},
			},
		},
	}),
}
streamHandler.AddEventSource(awslambdaeventsources.NewDynamoEventSource(slotMachineTable, &awslambdaeventsources.DynamoEventSourceProps{
	StartingPosition: awslambda.StartingPosition_LATEST,
	Enabled:          jsii.Bool(true),
	Filters:          &filters,
}))

Money saved

I wrote a CloudWatch Log Insights query [1] to discover how much I would save by filtering my Lambda function invocations to avoid Lambda invocations.

The total is around 1M Lambda invocations per week in this service, which at current pricing is around $0.20 per week. Since they only do a bit of filtering, the compute cost is negligible.

So, bear in mind that it may not be worth your time to do the work.