Skip to content

Instantly share code, notes, and snippets.

@timyhac
Created October 4, 2016 06:33
Show Gist options
  • Save timyhac/612e1d3ce72ac8d7ee2456dc1b7294f6 to your computer and use it in GitHub Desktop.
Save timyhac/612e1d3ce72ac8d7ee2456dc1b7294f6 to your computer and use it in GitHub Desktop.
AWS Lambda function to keep historical data for a table
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']))
@timyhac
Copy link
Author

timyhac commented Oct 4, 2016

The table should be created with the same Partition Key as the original table, and a numerical sort key of 'ApproximateCreationDateTime'

@timyhac
Copy link
Author

timyhac commented Oct 4, 2016

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