def handler(event, _): """ Lambda function handler for Products Table stream """ logger.debug({ "message": "Input event", "event": event }) logger.debug({ "message": "Records received", "records": event.get("Records", []) }) events = [ ddb_to_event(record, EVENT_BUS_NAME, "ecommerce.products", "Product", "productId") for record in event.get("Records", []) ] logger.info("Received %d event(s)", len(events)) logger.debug({ "message": "Events processed from records", "events": events }) send_events(events)
def test_ddb_to_event_modify(): """ Test ddb_to_event() with a MODIFY record """ event_bus_name = "EVENT_BUS_NAME" source = "SOURCE" object_type = "Object" resource_key = "pk" record = { "awsRegion": "eu-west-1", "dynamodb": { "Keys": { "pk": {"N": "123"}, "sk": {"N": "456"} }, "NewImage": { "SomeKey": {"S": "SomeValue"} }, "OldImage": { "SomeOtherKey": {"N": "123456"} }, "SequenceNumber": "1234567890123456789012345", "SizeBytes": 123, "StreamViewType": "NEW_AND_OLD_IMAGES" }, "eventID": str(uuid.uuid4()), "eventName": "REMOVE", "eventSource": "aws:dynamodb", "eventVersion": "1.0" } event = { "Source": source, "Resources": ["123"], "DetailType": "ObjectModified", "Detail": { "new": {"SomeKey": "SomeValue"}, "old": {"SomeOtherKey": 123456} }, "EventBusName": event_bus_name } retval = eventbridge.ddb_to_event(record, event_bus_name, source, object_type, resource_key) for key, value in event.items(): assert key in retval if key == "Detail": assert json.loads(value) == event[key] else: assert value == event[key]