15

I currently have an AWS DynamoDB stream triggers a Lambda function.

The Lambda function is triggered by both insert and update events in the DynamoDB. Is there a way to change the configuration so that the Lambda function would only be triggered by 'insert'?

C.Lee
  • 10,451
  • 9
  • 31
  • 46

5 Answers5

22

To my knowledge this is not possible. AWS Lambda polls the stream and invokes your Lambda function when it detects any type of stream record update. Your Lambda will have to ignore the records that you are not interested in. You can use the eventName property of the stream record (can have values INSERT | MODIFY | REMOVE)

Peter Fennema
  • 1,650
  • 12
  • 14
6

You can use your lambda function to ignore rest other than insert.

 for record in event.get('Records'):
    if record.get('eventName') in ('INSERT'):
       """ code for execution. """

    elif record.get('eventName') == 'REMOVE':
        pass
    elif record.get('eventName') ==  'MODIFY':
        pass
Antash
  • 878
  • 6
  • 15
Pirate
  • 91
  • 1
  • 6
0

In general, not only for nodejs but all types, this is supported for non global tables. Good collection of filter snippets can found here.

You basically apply filtering on Lambda service side before function gets triggered.

PS, Also great article explaining how it works

Andrey Borisko
  • 4,511
  • 2
  • 22
  • 31
0

While the accepted answer was accurate this is now supported by Lambda Event Filtering leveraging event source mappings.

aws lambda create-event-source-mapping \
    --function-name my-function \
    --event-source-arn arn:aws:sqs:us-east-2:123456789012:my-queue \
    --filter-criteria "{\"Filters\": [{\"Pattern\": \"{ \"a\" : [ 1, 2 ]}\"}]}"

Furthermore, you can include this in your Cloud formation or CDK workflow. See the accepted answer from this for more details.

ALFmachine
  • 639
  • 7
  • 12
0

You can use stream filters now and filter for those event types that you need only:

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.Lambda.Tutorial2.html

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34446906) – DSDmark May 27 '23 at 15:04