run dynamodb trigger for specific event types

Viewed 985

Everytime there's a change in the dynamo table, it runs a trigger which then runs a lambda function. For now, the trigger runs whenever i add or delete a file both. Instead, I only want to run the trigger if the event type is INSERT. How can I achieve this?

resource "aws_dynamodb_table" "filenames" {
  name           = local.dynamodb_table_filenames
  billing_mode   = "PROVISIONED"
  read_capacity  = 1000
  write_capacity = 1000
  hash_key       = "filename"
  stream_enabled = true
  stream_view_type = "NEW_IMAGE"

  #range_key      = ""

  attribute {
    name = "filename"
    type = "S"
  }

  tags = var.tags
}

resource "aws_lambda_event_source_mapping" "allow_dynamodb_table_to_trigger_lambda" {
  event_source_arn  = aws_dynamodb_table.filenames.stream_arn
  function_name     = aws_lambda_function.trigger_stepfunction_lambda.arn
  starting_position = "LATEST"
}

3 Answers

How can I achieve this?

You have to filter the events in your lambda function. A common patter is to have two functions. The first function, connected to dynamodb stream, receives all db events, filters them and then distributes to other consumers.

These consumers can be SNS, SQS other function and so on.

The alternative is to use CloudTrial data-plane events for DYnamoDB.

Sorry, unable to add a comment as I do not have enough reputation. I have been able to add a filter to a Dynamodb stream via the AWS Console. However, is this possible via Terraform? I have tried the example for "SQS with event filter" suggested in the below, but terraform errors out: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping

resource "aws_lambda_event_source_mapping" "example" {
  event_source_arn = aws_sqs_queue.sqs_queue_test.arn
  function_name    = aws_lambda_function.example.arn

  filter_criteria {
    filter {
      pattern = jsonencode({
        body = {
          Temperature : [{ numeric : [">", 0, "<=", 100] }]
          Location : ["New York"]
        }
      })
    }
  }
}

Error: Error: Unsupported block type │ │ on main.tf line 91, in resource "aws_lambda_event_source_mapping" "example": │ 91: filter_criteria { │ │ Blocks of type "filter_criteria" are not expected here.

Announced on 26 Nov 2021, now it is possible to filter events for Amazon SQS, Amazon DynamoDB, and Amazon Kinesis.

So in this case, is not required to implement the logic of filtering on lambda code, it can be done via Filter criteria (lambda configuration).

For example, to specify the lambda function should only be trigger on INSERT events, you use the following filter criteria:

{
    "eventName": [ "INSERT" ]
}

In the console appears in this way: enter image description here


Similarly, to filter UPDATE events, the filter is:

{
    "eventName": [ "MODIFY" ]
}

enter image description here

Announcement: https://aws.amazon.com/about-aws/whats-new/2021/11/aws-lambda-event-filtering-amazon-sqs-dynamodb-kinesis-sources/?nc1=h_ls

Documentation: https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html#filtering-syntax

Related