1

I have dynamo db which name as "test-dynamo"

I have enable Manage stream enter image description here

I need to capture in lambda function. If any update/Insert has happend to this dynamo table I need to see in the lambda

def gettable(table_name, dynamodb_client):
        response = dynamodb_client.get_item(
            TableName=table_name,
            Key={
                'id': {'S': key
                            }})
        return response

def lambda_handler(event, context):
    dynamodb_client = boto3.client('dynamodb')
    boto3.resource('dynamodb')
    res= gettable("test-dynamo",dynamodb_client )
    print('event', event )

2 Answers 2

1

You can checkout this documentation page for configuring the same.

for example once you subscirbe your lambda function to the corresponding dynamodb stream you create in cloudformation,

    rLoggingFunction:
        Type: AWS::Lambda::Function
        Properties:
        Runtime: python3.7
        Timeout: '300'
        Handler: index.handler
        Role: !GetAtt rLambdaRole.Arn
        Code:
            ZipFile: |
            import logging

            LOGGER = logging.getLogger()
            LOGGER.setLevel(logging.INFO)

            def handler(event, context):
                LOGGER.info('Received Event: %s', event)
                for rec in event['Records']:
                LOGGER.info('Record: %s', rec) 
    ....
    rDynamoDBTable:
        Type: AWS::DynamoDB::Table
        Properties:
        AttributeDefinitions:
            - AttributeName: id
            AttributeType: S
        KeySchema:
            - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
            ReadCapacityUnits: 1
            WriteCapacityUnits: 1
        StreamSpecification:
            StreamViewType: NEW_AND_OLD_IMAGES

    rDynamoDBTableStream:
        Type: AWS::Lambda::EventSourceMapping
        Properties:
        # The maximum number of DB items to send to Lambda
        BatchSize: 1
        Enabled: True
        EventSourceArn: !GetAtt rDynamoDBTable.StreamArn
        FunctionName: !GetAtt rLoggingFunction.Arn
        # Always start at the tail of the Stream
        StartingPosition: LATEST

There is a full example via serverless for the same.

Sign up to request clarification or add additional context in comments.

2 Comments

in python lambda, how to capture the dynamo db stream events
from documentation >Create an event source mapping in AWS Lambda. This event source mapping associates the DynamoDB stream with your Lambda function. After you create this event source mapping, AWS Lambda starts polling the stream. They will be delivered to your trigger like lambda we are configuring in this case automatically.
-1

You can do it using simple code once you enable stream in DynamoDB:

import json
import boto3
from boto3.dynamodb.conditions import Key
def lambda_handler(event, context):
      print('##Below Event occured')
      print(event)

This will print the latest event in cloudwatch logs.

Comments

Your Answer

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