0

I'm trying to connect a Lambda function as a trigger for a DynamoDB table.

In my serverless.yml file, I have defined a Lambda function and a DynamoDB table.

Question: How can I attach the Lambda function as a trigger to the DynamoDB?

My serverless.yaml (simplified):

functions:
  pushLeadEvent:
    handler: handler.pushLeadEvent
    events:
      - /* WHAT TO DO HERE? */

resources:
  Resources:
    leadEvent:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: leadEvent
        BillingMode: PAY_PER_REQUEST
        AttributeDefinitions:
          - AttributeName: owner
            AttributeType: S
          - AttributeName: timestamp
            AttributeType: N
        KeySchema:
          - AttributeName: owner
            KeyType: HASH
          - AttributeName: timestamp
            KeyType: RANGE

1 Answer 1

3

Based on Serverless Framework documentation and my personal experience, I would recommend this:

functions:
  pushLeadEvent:
    handler: handler.pushLeadEvent
    dependsOn:
     - leadEvent
    events:
     - stream:
       type: dynamodb
       arn: !GetAtt leadEvent.StreamArn
       batchSize: 100
       startingPosition: LATEST
       maximumRetryAttempts: 10
       bisectBatchOnFunctionError: true
       enabled: true

This will:

  • Subcribe your Lambda to all of the changes in leadEvent table.
  • Single batch can have up to 100 events.
  • Lambda will retry to process an event up to 10 times, if it fails
  • On each retry, if batch has more than 1 event, it will be splitted into 2 separated batches by half
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I get the error: EventSourceArn.split is not a function.
Could you try adding dependsOn: - leadEvent in function definition? Just under handler definition, on the same yaml level, leadEvent should be defined as first element of list, search "dependsOn" here for better example: serverless.com/framework/docs/providers/aws/guide/…
I got it to work. Needed to add dependsOn (as you mentioned), but also set type: dynamodb Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.