Created
October 4, 2016 06:33
-
-
Save timyhac/612e1d3ce72ac8d7ee2456dc1b7294f6 to your computer and use it in GitHub Desktop.
AWS Lambda function to keep historical data for a table
This file contains hidden or 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 __future__ import print_function | |
import json | |
import boto3 | |
import datetime | |
dynamodb = boto3.resource('dynamodb', region_name='ap-southeast-1') | |
table = dynamodb.Table('TableNameHistory') | |
def lambda_handler(event, context): | |
print("Received event: " + json.dumps(event, indent=2)) | |
for record in event['Records']: | |
item = {str(key): str(val['S']) for key, val in record['dynamodb']['NewImage'].items()} | |
item['ApproximateCreationDateTime'] = record['dynamodb']['ApproximateCreationDateTime'] | |
response = table.put_item(Item=item) | |
return 'Successfully processed {} records.'.format(len(event['Records'])) |
ApproximateCreationDateTime can be converted to datetime downstream with:
import datetime
epoch = datetime.datetime.utcfromtimestamp(ApproximateCreationDateTime)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The table should be created with the same Partition Key as the original table, and a numerical sort key of 'ApproximateCreationDateTime'