Created
August 31, 2020 17:01
-
-
Save brysontyrrell/4191830f1e6a27b2866a13341850b7f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import json | |
import os | |
import boto3 | |
EVENT_BUS = os.getenv("EVENT_BUS") | |
events_client = boto3.client("events") | |
def lambda_handler(event, context): | |
"""This Lambda function takes DynamoDB stream events and publishes them to an | |
EventBridge EventBus in batches (DynamoDB streams can be submitted in batches of a | |
maximum of 10). | |
""" | |
events_to_put = [] | |
for record in event["Records"]: | |
print(f"Event: {record['eventName']}/{record['eventID']}") | |
table_arn, _ = record["eventSourceARN"].split("/stream") | |
events_to_put.append( | |
{ | |
"Time": datetime.utcfromtimestamp( | |
record["dynamodb"]["ApproximateCreationDateTime"] | |
), | |
"Source": "my-service.database", | |
"Resources": [table_arn], | |
"DetailType": record["eventName"], | |
"Detail": json.dumps(record), # Gotcha here: Decimal() objects require handling | |
"EventBusName": EVENT_BUS, | |
} | |
) | |
events_client.put_events(Entries=events_to_put) | |
return "ok" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment