Skip to content

Instantly share code, notes, and snippets.

@haranjackson
Created July 5, 2019 22:34
Show Gist options
  • Save haranjackson/70e2849e2bc327df87aa0a94b96d433e to your computer and use it in GitHub Desktop.
Save haranjackson/70e2849e2bc327df87aa0a94b96d433e to your computer and use it in GitHub Desktop.
Transfers the all the entries from one DynamoDB table to another
from boto3 import resource
OLD_TABLE = ''
NEW_TABLE = ''
OLD_REGION = ''
NEW_REGION = ''
def put_new_items(response, newTable):
items = response['Items']
with newTable.batch_writer() as batch:
for item in items:
batch.put_item(Item=item)
return len(items)
if __name__ == '__main__':
ddbOld = resource('dynamodb', region_name=OLD_REGION)
ddbNew = resource('dynamodb', region_name=NEW_REGION)
oldTable = ddbOld.Table(OLD_TABLE)
newTable = ddbNew.Table(NEW_TABLE)
count = 0
response = oldTable.scan()
count += put_new_items(response, newTable)
while 'LastEvaluatedKey' in response:
response = oldTable.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
count += put_new_items(response, newTable)
print(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment