Reading stream from DynamoDB by Lambda function declared using SAM

An example of how to read stream from DynamoDB by Lambda function declared using template.yaml of your SAM configuration

YourLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Description: Description of your function
      Runtime: nodejs16.x
      Architectures:
        - x86_64
      Handler: index.handler
      Events:
        Stream:
          Type: DynamoDB
          Properties:
            Stream: !GetAtt YourDynamoDbResourceName.StreamArn
            BatchSize: 1
            MaximumRetryAttempts: 3
            DestinationConfig:
              OnFailure:
                Destination: !GetAtt YourSQSResourceName.Arn
            StartingPosition: TRIM_HORIZON
      MemorySize: 128
      Timeout: 100

With this example, your Lambda function will be executed dynamically when there will be new messages in your DynamoDB stream. In case some error happens, this example includes 3 retries. If none of these was successful Lambda will pass your message to specified SQS queue.

Schema to create a resource of DynamoDB table using SAMSchema to create a resource of SQS queue using SAM

More info on the available properties for the Lambda function creation is here. I use CDK documentation, as I have not found related to SAM resources.

Last updated