"""Handle DynamoDB Streams events. 1. Read a set of DDB Streams events. 2. Execute other lambda to handle event. """ import boto3 import json import common_config import config from constants import fields import exceptions @exceptions.sentry_capture_exception def handler(event, context): """Handle dynamoDB events, for each call the Lambda that saves to DD DB.""" 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] common_config.logger.debug('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() )