> For the complete documentation index, see [llms.txt](https://tricks.bodik.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tricks.bodik.tech/infrastructure/aws/sam/reading-stream-from-dynamodb-by-lambda-function-declared-using-sam.md).

# 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

```yaml
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.&#x20;

{% content-ref url="/pages/mLbqZfBzTa9cL9XhIRII" %}
[Schema to create a resource of DynamoDB table using SAM](/infrastructure/aws/sam/schema-to-create-a-resource-of-dynamodb-table-using-sam.md)
{% endcontent-ref %}

{% content-ref url="/pages/lwHfmmkRtuOLaLxWXIY2" %}
[Schema to create a resource of SQS queue using SAM](/infrastructure/aws/sam/schema-to-create-a-resource-of-sqs-queue-using-sam.md)
{% endcontent-ref %}

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.

{% embed url="<https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-lambda.Function.html>" %}
