Home > Enterprise >  Trigger the lambda with dynamodb when a specific entry is registered
Trigger the lambda with dynamodb when a specific entry is registered

Time:03-10

I need to trigger a lambda when a specific object registers on DynamoDB.

For example:

If I create a User with a POST /my-website/user and, I store this User on DynamoDB, I want to trigger my Lambda.

I don't want to trigger the Lambda if the registered object is different from the User.

enter image description here

For the management of my stack, I use Serverless (with a serverless.yml file) and CloudFormation syntax.

With the serverless documentation, I can't figure out how I can trigger my Lambda only when a specific entry is registered to DynamoDB ( https://www.serverless.com/framework/docs/providers/aws/events/streams ).

Thanks in advance,

EDIT:

Thank you for your answers :)

It's work:

  statement:
    handler: lambda/statement.php
    layers:
      - arn:aws:lambda:#{AWS::Region}:<account_id>:layer:php-73:1
    iamRoleStatements:
      - Effect: Allow
        Action:
          - dynamodb:ListStreams
          - dynamodb:GetItem
    events:
    - stream:
        type: dynamodb
        arn: arn:aws:dynamodb:eu-west-3:<account_id>:table/dev-project/stream/2020-11-18T22:34:01.579
        maximumRetryAttempts: 1
        batchSize: 1
        filterPatterns:
          - eventName: [INSERT]
            dynamodb:
              NewImage:
                __partitionKey:
                  S: [myPk]

CodePudding user response:

You have to setup stream filters. The process is explained in:

CodePudding user response:

Stream Filters worked (thank you). Here's the final configuration:

  statement:
    handler: lambda/statement.php
    layers:
      - arn:aws:lambda:#{AWS::Region}:<account_id>:layer:php-73:1
    iamRoleStatements:
      - Effect: Allow
        Action:
          - dynamodb:ListStreams
          - dynamodb:GetItem
    events:
    - stream:
        type: dynamodb
        arn: arn:aws:dynamodb:eu-west-3:<account_id>:table/dev-project/stream/2020-11-18T22:34:01.579
        maximumRetryAttempts: 1
        batchSize: 1
        filterPatterns:
          - eventName: [INSERT]
            dynamodb:
              NewImage:
                __partitionKey:
                  S: [myPk]
  • Related