"""Handle DynamoDB Streams events. 1. Read a set of DDB Streams events. 2. Execute other lambda to handle event. """ import json import boto3 from sentry_sdk import capture_exception from src import config from constants import fields def handler(event, context): """Handle dynamoDB events, for each call the Lambda that saves to DD DB.""" try: if event is None or fields.DDB_EVENT_RECORDS not in event: return client = boto3.client('lambda') for record in event[fields.DDB_EVENT_RECORDS]: # Get a part of the current record with new values only. if fields.DDB_EVENT_RECORD_MAIN not in record: continue ddb = record[fields.DDB_EVENT_RECORD_MAIN] if fields.DDB_EVENT_RECORD_NEW_VALUES not in ddb: continue new_item = ddb[fields.DDB_EVENT_RECORD_NEW_VALUES] config.logger.info('Processing record: %s', new_item) # Using synchronous (blocking) invocation here is important - we want # to save incoming events/records preserving the order. client.invoke( FunctionName=config.VO_TO_DD_SYNC_LAMBDA_NAME, Payload=json.dumps(new_item).encode() ) except Exception as e: capture_exception(e) raise e